Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2017 Intel Corporation.
3 : : * All rights reserved.
4 : : * Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "spdk/stdinc.h"
8 : :
9 : : #include "spdk/blob.h"
10 : : #include "spdk/crc32.h"
11 : : #include "spdk/env.h"
12 : : #include "spdk/queue.h"
13 : : #include "spdk/thread.h"
14 : : #include "spdk/bit_array.h"
15 : : #include "spdk/bit_pool.h"
16 : : #include "spdk/likely.h"
17 : : #include "spdk/util.h"
18 : : #include "spdk/string.h"
19 : :
20 : : #include "spdk_internal/assert.h"
21 : : #include "spdk/log.h"
22 : :
23 : : #include "blobstore.h"
24 : :
25 : : #define BLOB_CRC32C_INITIAL 0xffffffffUL
26 : :
27 : : static int bs_register_md_thread(struct spdk_blob_store *bs);
28 : : static int bs_unregister_md_thread(struct spdk_blob_store *bs);
29 : : static void blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
30 : : static void blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
31 : : uint64_t cluster, uint32_t extent, struct spdk_blob_md_page *page,
32 : : spdk_blob_op_complete cb_fn, void *cb_arg);
33 : : static void blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
34 : : uint32_t extent_page, struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
35 : :
36 : : static int blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
37 : : uint16_t value_len, bool internal);
38 : : static int blob_get_xattr_value(struct spdk_blob *blob, const char *name,
39 : : const void **value, size_t *value_len, bool internal);
40 : : static int blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal);
41 : :
42 : : static void blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
43 : : struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
44 : : static void blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg);
45 : :
46 : : static void bs_shallow_copy_cluster_find_next(void *cb_arg);
47 : :
48 : : /*
49 : : * External snapshots require a channel per thread per esnap bdev. The tree
50 : : * is populated lazily as blob IOs are handled by the back_bs_dev. When this
51 : : * channel is destroyed, all the channels in the tree are destroyed.
52 : : */
53 : :
54 : : struct blob_esnap_channel {
55 : : RB_ENTRY(blob_esnap_channel) node;
56 : : spdk_blob_id blob_id;
57 : : struct spdk_io_channel *channel;
58 : : };
59 : :
60 : : static int blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2);
61 : : static void blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
62 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg);
63 : : static void blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch);
64 : : static void blob_set_back_bs_dev_frozen(void *_ctx, int bserrno);
65 [ + + + + : 51322 : RB_GENERATE_STATIC(blob_esnap_channel_tree, blob_esnap_channel, node, blob_esnap_channel_compare)
+ + + - +
+ + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + #
# # # # #
# # # # #
# # # ]
66 : :
67 : : static inline bool
68 : 7471586 : blob_is_esnap_clone(const struct spdk_blob *blob)
69 : : {
70 [ - + ]: 7471586 : assert(blob != NULL);
71 : 7471586 : return !!(blob->invalid_flags & SPDK_BLOB_EXTERNAL_SNAPSHOT);
72 : : }
73 : :
74 : : static int
75 : 31106 : blob_id_cmp(struct spdk_blob *blob1, struct spdk_blob *blob2)
76 : : {
77 [ + - + - ]: 31106 : assert(blob1 != NULL && blob2 != NULL);
78 [ + + ]: 31106 : return (blob1->id < blob2->id ? -1 : blob1->id > blob2->id);
79 : : }
80 : :
81 [ + + + + : 108878 : RB_GENERATE_STATIC(spdk_blob_tree, spdk_blob, link, blob_id_cmp);
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + + +
+ + + ]
82 : :
83 : : static void
84 : 669340 : blob_verify_md_op(struct spdk_blob *blob)
85 : : {
86 [ - + ]: 669340 : assert(blob != NULL);
87 [ - + ]: 669340 : assert(spdk_get_thread() == blob->bs->md_thread);
88 [ - + ]: 669340 : assert(blob->state != SPDK_BLOB_STATE_LOADING);
89 : 669340 : }
90 : :
91 : : static struct spdk_blob_list *
92 : 16070 : bs_get_snapshot_entry(struct spdk_blob_store *bs, spdk_blob_id blobid)
93 : : {
94 : 16070 : struct spdk_blob_list *snapshot_entry = NULL;
95 : :
96 [ + + ]: 19363 : TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
97 [ + + ]: 5944 : if (snapshot_entry->id == blobid) {
98 : 2651 : break;
99 : : }
100 : : }
101 : :
102 : 16070 : return snapshot_entry;
103 : : }
104 : :
105 : : static void
106 : 11363 : bs_claim_md_page(struct spdk_blob_store *bs, uint32_t page)
107 : : {
108 [ - + ]: 11363 : assert(spdk_spin_held(&bs->used_lock));
109 [ - + ]: 11363 : assert(page < spdk_bit_array_capacity(bs->used_md_pages));
110 [ - + ]: 11363 : assert(spdk_bit_array_get(bs->used_md_pages, page) == false);
111 : :
112 : 11363 : spdk_bit_array_set(bs->used_md_pages, page);
113 : 11363 : }
114 : :
115 : : static void
116 : 8426 : bs_release_md_page(struct spdk_blob_store *bs, uint32_t page)
117 : : {
118 [ - + ]: 8426 : assert(spdk_spin_held(&bs->used_lock));
119 [ - + ]: 8426 : assert(page < spdk_bit_array_capacity(bs->used_md_pages));
120 [ - + ]: 8426 : assert(spdk_bit_array_get(bs->used_md_pages, page) == true);
121 : :
122 : 8426 : spdk_bit_array_clear(bs->used_md_pages, page);
123 : 8426 : }
124 : :
125 : : static uint32_t
126 : 356257 : bs_claim_cluster(struct spdk_blob_store *bs)
127 : : {
128 : : uint32_t cluster_num;
129 : :
130 [ - + ]: 356257 : assert(spdk_spin_held(&bs->used_lock));
131 : :
132 : 356257 : cluster_num = spdk_bit_pool_allocate_bit(bs->used_clusters);
133 [ + + ]: 356257 : if (cluster_num == UINT32_MAX) {
134 : 1 : return UINT32_MAX;
135 : : }
136 : :
137 [ - + - + ]: 356256 : SPDK_DEBUGLOG(blob, "Claiming cluster %u\n", cluster_num);
138 : 356256 : bs->num_free_clusters--;
139 : :
140 : 356256 : return cluster_num;
141 : : }
142 : :
143 : : static void
144 : 308405 : bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
145 : : {
146 [ - + ]: 308405 : assert(spdk_spin_held(&bs->used_lock));
147 [ - + ]: 308405 : assert(cluster_num < spdk_bit_pool_capacity(bs->used_clusters));
148 [ - + ]: 308405 : assert(spdk_bit_pool_is_allocated(bs->used_clusters, cluster_num) == true);
149 [ - + ]: 308405 : assert(bs->num_free_clusters < bs->total_clusters);
150 : :
151 [ - + - + ]: 308405 : SPDK_DEBUGLOG(blob, "Releasing cluster %u\n", cluster_num);
152 : :
153 : 308405 : spdk_bit_pool_free_bit(bs->used_clusters, cluster_num);
154 : 308405 : bs->num_free_clusters++;
155 : 308405 : }
156 : :
157 : : static int
158 : 356256 : blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster)
159 : : {
160 : 356256 : uint64_t *cluster_lba = &blob->active.clusters[cluster_num];
161 : :
162 : 356256 : blob_verify_md_op(blob);
163 : :
164 [ + + ]: 356256 : if (*cluster_lba != 0) {
165 : 20 : return -EEXIST;
166 : : }
167 : :
168 : 356236 : *cluster_lba = bs_cluster_to_lba(blob->bs, cluster);
169 : 356236 : blob->active.num_allocated_clusters++;
170 : :
171 : 356236 : return 0;
172 : : }
173 : :
174 : : static int
175 : 356257 : bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num,
176 : : uint64_t *cluster, uint32_t *lowest_free_md_page, bool update_map)
177 : : {
178 : 356257 : uint32_t *extent_page = 0;
179 : :
180 [ - + ]: 356257 : assert(spdk_spin_held(&blob->bs->used_lock));
181 : :
182 : 356257 : *cluster = bs_claim_cluster(blob->bs);
183 [ + + ]: 356257 : if (*cluster == UINT32_MAX) {
184 : : /* No more free clusters. Cannot satisfy the request */
185 : 1 : return -ENOSPC;
186 : : }
187 : :
188 [ + + + + ]: 356256 : if (blob->use_extent_table) {
189 : 344100 : extent_page = bs_cluster_to_extent_page(blob, cluster_num);
190 [ + + ]: 344100 : if (*extent_page == 0) {
191 : : /* Extent page shall never occupy md_page so start the search from 1 */
192 [ + + ]: 3654 : if (*lowest_free_md_page == 0) {
193 : 3174 : *lowest_free_md_page = 1;
194 : : }
195 : : /* No extent_page is allocated for the cluster */
196 : 3654 : *lowest_free_md_page = spdk_bit_array_find_first_clear(blob->bs->used_md_pages,
197 : : *lowest_free_md_page);
198 [ - + ]: 3654 : if (*lowest_free_md_page == UINT32_MAX) {
199 : : /* No more free md pages. Cannot satisfy the request */
200 : 0 : bs_release_cluster(blob->bs, *cluster);
201 : 0 : return -ENOSPC;
202 : : }
203 : 3654 : bs_claim_md_page(blob->bs, *lowest_free_md_page);
204 : : }
205 : : }
206 : :
207 [ - + - + ]: 356256 : SPDK_DEBUGLOG(blob, "Claiming cluster %" PRIu64 " for blob 0x%" PRIx64 "\n", *cluster,
208 : : blob->id);
209 : :
210 [ + + ]: 356256 : if (update_map) {
211 : 352030 : blob_insert_cluster(blob, cluster_num, *cluster);
212 [ + + + + : 352030 : if (blob->use_extent_table && *extent_page == 0) {
+ + ]
213 : 3336 : *extent_page = *lowest_free_md_page;
214 : : }
215 : : }
216 : :
217 : 356256 : return 0;
218 : : }
219 : :
220 : : static void
221 : 19383 : blob_xattrs_init(struct spdk_blob_xattr_opts *xattrs)
222 : : {
223 : 19383 : xattrs->count = 0;
224 : 19383 : xattrs->names = NULL;
225 : 19383 : xattrs->ctx = NULL;
226 : 19383 : xattrs->get_value = NULL;
227 : 19383 : }
228 : :
229 : : void
230 : 12530 : spdk_blob_opts_init(struct spdk_blob_opts *opts, size_t opts_size)
231 : : {
232 [ - + ]: 12530 : if (!opts) {
233 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
234 : 0 : return;
235 : : }
236 : :
237 [ - + ]: 12530 : if (!opts_size) {
238 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
239 : 0 : return;
240 : : }
241 : :
242 [ - + ]: 12530 : memset(opts, 0, opts_size);
243 : 12530 : opts->opts_size = opts_size;
244 : :
245 : : #define FIELD_OK(field) \
246 : : offsetof(struct spdk_blob_opts, field) + sizeof(opts->field) <= opts_size
247 : :
248 : : #define SET_FIELD(field, value) \
249 : : if (FIELD_OK(field)) { \
250 : : opts->field = value; \
251 : : } \
252 : :
253 [ + - ]: 12530 : SET_FIELD(num_clusters, 0);
254 [ + - ]: 12530 : SET_FIELD(thin_provision, false);
255 [ + - ]: 12530 : SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
256 : :
257 [ + - ]: 12530 : if (FIELD_OK(xattrs)) {
258 : 12530 : blob_xattrs_init(&opts->xattrs);
259 : : }
260 : :
261 [ + - ]: 12530 : SET_FIELD(use_extent_table, true);
262 : :
263 : : #undef FIELD_OK
264 : : #undef SET_FIELD
265 : : }
266 : :
267 : : void
268 : 17110 : spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts, size_t opts_size)
269 : : {
270 [ - + ]: 17110 : if (!opts) {
271 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
272 : 0 : return;
273 : : }
274 : :
275 [ - + ]: 17110 : if (!opts_size) {
276 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
277 : 0 : return;
278 : : }
279 : :
280 [ - + ]: 17110 : memset(opts, 0, opts_size);
281 : 17110 : opts->opts_size = opts_size;
282 : :
283 : : #define FIELD_OK(field) \
284 : : offsetof(struct spdk_blob_open_opts, field) + sizeof(opts->field) <= opts_size
285 : :
286 : : #define SET_FIELD(field, value) \
287 : : if (FIELD_OK(field)) { \
288 : : opts->field = value; \
289 : : } \
290 : :
291 [ + - ]: 17110 : SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
292 : :
293 : : #undef FIELD_OK
294 : : #undef SET_FILED
295 : : }
296 : :
297 : : static struct spdk_blob *
298 : 23559 : blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id)
299 : : {
300 : : struct spdk_blob *blob;
301 : :
302 : 23559 : blob = calloc(1, sizeof(*blob));
303 [ - + ]: 23559 : if (!blob) {
304 : 0 : return NULL;
305 : : }
306 : :
307 : 23559 : blob->id = id;
308 : 23559 : blob->bs = bs;
309 : :
310 : 23559 : blob->parent_id = SPDK_BLOBID_INVALID;
311 : :
312 : 23559 : blob->state = SPDK_BLOB_STATE_DIRTY;
313 : 23559 : blob->extent_rle_found = false;
314 : 23559 : blob->extent_table_found = false;
315 : 23559 : blob->active.num_pages = 1;
316 : 23559 : blob->active.pages = calloc(1, sizeof(*blob->active.pages));
317 [ - + ]: 23559 : if (!blob->active.pages) {
318 : 0 : free(blob);
319 : 0 : return NULL;
320 : : }
321 : :
322 : 23559 : blob->active.pages[0] = bs_blobid_to_page(id);
323 : :
324 : 23559 : TAILQ_INIT(&blob->xattrs);
325 : 23559 : TAILQ_INIT(&blob->xattrs_internal);
326 : 23559 : TAILQ_INIT(&blob->pending_persists);
327 : 23559 : TAILQ_INIT(&blob->persists_to_complete);
328 : :
329 : 23559 : return blob;
330 : : }
331 : :
332 : : static void
333 : 47118 : xattrs_free(struct spdk_xattr_tailq *xattrs)
334 : : {
335 : : struct spdk_xattr *xattr, *xattr_tmp;
336 : :
337 [ + + ]: 65890 : TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) {
338 [ + + ]: 18772 : TAILQ_REMOVE(xattrs, xattr, link);
339 : 18772 : free(xattr->name);
340 : 18772 : free(xattr->value);
341 : 18772 : free(xattr);
342 : : }
343 : 47118 : }
344 : :
345 : : static void
346 : 23559 : blob_free(struct spdk_blob *blob)
347 : : {
348 [ - + ]: 23559 : assert(blob != NULL);
349 [ - + ]: 23559 : assert(TAILQ_EMPTY(&blob->pending_persists));
350 [ - + ]: 23559 : assert(TAILQ_EMPTY(&blob->persists_to_complete));
351 : :
352 : 23559 : free(blob->active.extent_pages);
353 : 23559 : free(blob->clean.extent_pages);
354 : 23559 : free(blob->active.clusters);
355 : 23559 : free(blob->clean.clusters);
356 : 23559 : free(blob->active.pages);
357 : 23559 : free(blob->clean.pages);
358 : :
359 : 23559 : xattrs_free(&blob->xattrs);
360 : 23559 : xattrs_free(&blob->xattrs_internal);
361 : :
362 [ + + ]: 23559 : if (blob->back_bs_dev) {
363 : 3610 : blob->back_bs_dev->destroy(blob->back_bs_dev);
364 : : }
365 : :
366 : 23559 : free(blob);
367 : 23559 : }
368 : :
369 : : static void
370 : 1011 : blob_back_bs_destroy_esnap_done(void *ctx, struct spdk_blob *blob, int bserrno)
371 : : {
372 : 1011 : struct spdk_bs_dev *bs_dev = ctx;
373 : :
374 [ - + ]: 1011 : if (bserrno != 0) {
375 : : /*
376 : : * This is probably due to a memory allocation failure when creating the
377 : : * blob_esnap_destroy_ctx before iterating threads.
378 : : */
379 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": Unable to destroy bs dev channels: error %d\n",
380 : : blob->id, bserrno);
381 : 0 : assert(false);
382 : : }
383 : :
384 [ - + ]: 1011 : if (bs_dev == NULL) {
385 : : /*
386 : : * This check exists to make scanbuild happy.
387 : : *
388 : : * blob->back_bs_dev for an esnap is NULL during the first iteration of blobs while
389 : : * the blobstore is being loaded. It could also be NULL if there was an error
390 : : * opening the esnap device. In each of these cases, no channels could have been
391 : : * created because back_bs_dev->create_channel() would have led to a NULL pointer
392 : : * deref.
393 : : */
394 : 0 : assert(false);
395 : : return;
396 : : }
397 : :
398 [ - + - + ]: 1011 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": calling destroy on back_bs_dev\n", blob->id);
399 : 1011 : bs_dev->destroy(bs_dev);
400 : : }
401 : :
402 : : static void
403 : 1011 : blob_back_bs_destroy(struct spdk_blob *blob)
404 : : {
405 [ - + - + ]: 1011 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": preparing to destroy back_bs_dev\n",
406 : : blob->id);
407 : :
408 : 1011 : blob_esnap_destroy_bs_dev_channels(blob, false, blob_back_bs_destroy_esnap_done,
409 : 1011 : blob->back_bs_dev);
410 : 1011 : blob->back_bs_dev = NULL;
411 : 1011 : }
412 : :
413 : : struct blob_parent {
414 : : union {
415 : : struct {
416 : : spdk_blob_id id;
417 : : struct spdk_blob *blob;
418 : : } snapshot;
419 : :
420 : : struct {
421 : : void *id;
422 : : uint32_t id_len;
423 : : struct spdk_bs_dev *back_bs_dev;
424 : : } esnap;
425 : : } u;
426 : : };
427 : :
428 : : typedef int (*set_parent_refs_cb)(struct spdk_blob *blob, struct blob_parent *parent);
429 : :
430 : : struct set_bs_dev_ctx {
431 : : struct spdk_blob *blob;
432 : : struct spdk_bs_dev *back_bs_dev;
433 : :
434 : : /*
435 : : * This callback is used during a set parent operation to change the references
436 : : * to the parent of the blob.
437 : : */
438 : : set_parent_refs_cb parent_refs_cb_fn;
439 : : struct blob_parent *parent_refs_cb_arg;
440 : :
441 : : spdk_blob_op_complete cb_fn;
442 : : void *cb_arg;
443 : : int bserrno;
444 : : };
445 : :
446 : : static void
447 : 93 : blob_set_back_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
448 : : set_parent_refs_cb parent_refs_cb_fn, struct blob_parent *parent_refs_cb_arg,
449 : : spdk_blob_op_complete cb_fn, void *cb_arg)
450 : : {
451 : : struct set_bs_dev_ctx *ctx;
452 : :
453 : 93 : ctx = calloc(1, sizeof(*ctx));
454 [ - + ]: 93 : if (ctx == NULL) {
455 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": out of memory while setting back_bs_dev\n",
456 : : blob->id);
457 : 0 : cb_fn(cb_arg, -ENOMEM);
458 : 0 : return;
459 : : }
460 : :
461 : 93 : ctx->parent_refs_cb_fn = parent_refs_cb_fn;
462 : 93 : ctx->parent_refs_cb_arg = parent_refs_cb_arg;
463 : 93 : ctx->cb_fn = cb_fn;
464 : 93 : ctx->cb_arg = cb_arg;
465 : 93 : ctx->back_bs_dev = back_bs_dev;
466 : 93 : ctx->blob = blob;
467 : :
468 : 93 : blob_freeze_io(blob, blob_set_back_bs_dev_frozen, ctx);
469 : : }
470 : :
471 : : struct freeze_io_ctx {
472 : : struct spdk_bs_cpl cpl;
473 : : struct spdk_blob *blob;
474 : : };
475 : :
476 : : static void
477 : 28319 : blob_io_sync(struct spdk_io_channel_iter *i)
478 : : {
479 : 28319 : spdk_for_each_channel_continue(i, 0);
480 : 28319 : }
481 : :
482 : : static void
483 : 28283 : blob_execute_queued_io(struct spdk_io_channel_iter *i)
484 : : {
485 : 28283 : struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
486 : 28283 : struct spdk_bs_channel *ch = spdk_io_channel_get_ctx(_ch);
487 : 28283 : struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
488 : : struct spdk_bs_request_set *set;
489 : : struct spdk_bs_user_op_args *args;
490 : : spdk_bs_user_op_t *op, *tmp;
491 : :
492 [ + + ]: 29888 : TAILQ_FOREACH_SAFE(op, &ch->queued_io, link, tmp) {
493 : 1605 : set = (struct spdk_bs_request_set *)op;
494 : 1605 : args = &set->u.user_op;
495 : :
496 [ + - ]: 1605 : if (args->blob == ctx->blob) {
497 [ + + ]: 1605 : TAILQ_REMOVE(&ch->queued_io, op, link);
498 : 1605 : bs_user_op_execute(op);
499 : : }
500 : : }
501 : :
502 : 28283 : spdk_for_each_channel_continue(i, 0);
503 : 28283 : }
504 : :
505 : : static void
506 : 56458 : blob_io_cpl(struct spdk_io_channel_iter *i, int status)
507 : : {
508 : 56458 : struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
509 : :
510 : 56458 : ctx->cpl.u.blob_basic.cb_fn(ctx->cpl.u.blob_basic.cb_arg, 0);
511 : :
512 : 56458 : free(ctx);
513 : 56458 : }
514 : :
515 : : static void
516 : 28247 : blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
517 : : {
518 : : struct freeze_io_ctx *ctx;
519 : :
520 : 28247 : blob_verify_md_op(blob);
521 : :
522 : 28247 : ctx = calloc(1, sizeof(*ctx));
523 [ - + ]: 28247 : if (!ctx) {
524 : 0 : cb_fn(cb_arg, -ENOMEM);
525 : 0 : return;
526 : : }
527 : :
528 : 28247 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
529 : 28247 : ctx->cpl.u.blob_basic.cb_fn = cb_fn;
530 : 28247 : ctx->cpl.u.blob_basic.cb_arg = cb_arg;
531 : 28247 : ctx->blob = blob;
532 : :
533 : : /* Freeze I/O on blob */
534 : 28247 : blob->frozen_refcnt++;
535 : :
536 : 28247 : spdk_for_each_channel(blob->bs, blob_io_sync, ctx, blob_io_cpl);
537 : : }
538 : :
539 : : static void
540 : 28211 : blob_unfreeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
541 : : {
542 : : struct freeze_io_ctx *ctx;
543 : :
544 : 28211 : blob_verify_md_op(blob);
545 : :
546 : 28211 : ctx = calloc(1, sizeof(*ctx));
547 [ - + ]: 28211 : if (!ctx) {
548 : 0 : cb_fn(cb_arg, -ENOMEM);
549 : 0 : return;
550 : : }
551 : :
552 : 28211 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
553 : 28211 : ctx->cpl.u.blob_basic.cb_fn = cb_fn;
554 : 28211 : ctx->cpl.u.blob_basic.cb_arg = cb_arg;
555 : 28211 : ctx->blob = blob;
556 : :
557 [ - + ]: 28211 : assert(blob->frozen_refcnt > 0);
558 : :
559 : 28211 : blob->frozen_refcnt--;
560 : :
561 : 28211 : spdk_for_each_channel(blob->bs, blob_execute_queued_io, ctx, blob_io_cpl);
562 : : }
563 : :
564 : : static int
565 : 72469 : blob_mark_clean(struct spdk_blob *blob)
566 : : {
567 : 72469 : uint32_t *extent_pages = NULL;
568 : 72469 : uint64_t *clusters = NULL;
569 : 72469 : uint32_t *pages = NULL;
570 : :
571 [ - + ]: 72469 : assert(blob != NULL);
572 : :
573 [ + + ]: 72469 : if (blob->active.num_extent_pages) {
574 [ - + ]: 52746 : assert(blob->active.extent_pages);
575 : 52746 : extent_pages = calloc(blob->active.num_extent_pages, sizeof(*blob->active.extent_pages));
576 [ - + ]: 52746 : if (!extent_pages) {
577 : 0 : return -ENOMEM;
578 : : }
579 [ - + - + ]: 52746 : memcpy(extent_pages, blob->active.extent_pages,
580 : 52746 : blob->active.num_extent_pages * sizeof(*extent_pages));
581 : : }
582 : :
583 [ + + ]: 72469 : if (blob->active.num_clusters) {
584 [ - + ]: 61965 : assert(blob->active.clusters);
585 : 61965 : clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters));
586 [ - + ]: 61965 : if (!clusters) {
587 : 0 : free(extent_pages);
588 : 0 : return -ENOMEM;
589 : : }
590 [ - + - + ]: 61965 : memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
591 : : }
592 : :
593 [ + + ]: 72469 : if (blob->active.num_pages) {
594 [ - + ]: 67272 : assert(blob->active.pages);
595 : 67272 : pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages));
596 [ - + ]: 67272 : if (!pages) {
597 : 0 : free(extent_pages);
598 : 0 : free(clusters);
599 : 0 : return -ENOMEM;
600 : : }
601 [ - + - + ]: 67272 : memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
602 : : }
603 : :
604 : 72469 : free(blob->clean.extent_pages);
605 : 72469 : free(blob->clean.clusters);
606 : 72469 : free(blob->clean.pages);
607 : :
608 : 72469 : blob->clean.num_extent_pages = blob->active.num_extent_pages;
609 : 72469 : blob->clean.extent_pages = blob->active.extent_pages;
610 : 72469 : blob->clean.num_clusters = blob->active.num_clusters;
611 : 72469 : blob->clean.clusters = blob->active.clusters;
612 : 72469 : blob->clean.num_allocated_clusters = blob->active.num_allocated_clusters;
613 : 72469 : blob->clean.num_pages = blob->active.num_pages;
614 : 72469 : blob->clean.pages = blob->active.pages;
615 : :
616 : 72469 : blob->active.extent_pages = extent_pages;
617 : 72469 : blob->active.clusters = clusters;
618 : 72469 : blob->active.pages = pages;
619 : :
620 : : /* If the metadata was dirtied again while the metadata was being written to disk,
621 : : * we do not want to revert the DIRTY state back to CLEAN here.
622 : : */
623 [ + + ]: 72469 : if (blob->state == SPDK_BLOB_STATE_LOADING) {
624 : 16514 : blob->state = SPDK_BLOB_STATE_CLEAN;
625 : : }
626 : :
627 : 72469 : return 0;
628 : : }
629 : :
630 : : static int
631 : 14906 : blob_deserialize_xattr(struct spdk_blob *blob,
632 : : struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal)
633 : : {
634 : : struct spdk_xattr *xattr;
635 : :
636 : 14906 : if (desc_xattr->length != sizeof(desc_xattr->name_length) +
637 : : sizeof(desc_xattr->value_length) +
638 [ - + ]: 14906 : desc_xattr->name_length + desc_xattr->value_length) {
639 : 0 : return -EINVAL;
640 : : }
641 : :
642 : 14906 : xattr = calloc(1, sizeof(*xattr));
643 [ - + ]: 14906 : if (xattr == NULL) {
644 : 0 : return -ENOMEM;
645 : : }
646 : :
647 : 14906 : xattr->name = malloc(desc_xattr->name_length + 1);
648 [ - + ]: 14906 : if (xattr->name == NULL) {
649 : 0 : free(xattr);
650 : 0 : return -ENOMEM;
651 : : }
652 : :
653 : 14906 : xattr->value = malloc(desc_xattr->value_length);
654 [ - + ]: 14906 : if (xattr->value == NULL) {
655 : 0 : free(xattr->name);
656 : 0 : free(xattr);
657 : 0 : return -ENOMEM;
658 : : }
659 : :
660 [ - + - + ]: 14906 : memcpy(xattr->name, desc_xattr->name, desc_xattr->name_length);
661 : 14906 : xattr->name[desc_xattr->name_length] = '\0';
662 : 14906 : xattr->value_len = desc_xattr->value_length;
663 [ - + - + ]: 14906 : memcpy(xattr->value,
664 : 14906 : (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
665 : 14906 : desc_xattr->value_length);
666 : :
667 [ + + + + : 14906 : TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link);
+ + ]
668 : :
669 : 14906 : return 0;
670 : : }
671 : :
672 : :
673 : : static int
674 : 25924 : blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob)
675 : : {
676 : : struct spdk_blob_md_descriptor *desc;
677 : 25924 : size_t cur_desc = 0;
678 : : void *tmp;
679 : :
680 : 25924 : desc = (struct spdk_blob_md_descriptor *)page->descriptors;
681 [ + - ]: 82086 : while (cur_desc < sizeof(page->descriptors)) {
682 [ + + ]: 82086 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
683 [ + - ]: 25780 : if (desc->length == 0) {
684 : : /* If padding and length are 0, this terminates the page */
685 : 25780 : break;
686 : : }
687 [ + + ]: 56306 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
688 : : struct spdk_blob_md_descriptor_flags *desc_flags;
689 : :
690 : 16610 : desc_flags = (struct spdk_blob_md_descriptor_flags *)desc;
691 : :
692 [ - + ]: 16610 : if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) {
693 : 0 : return -EINVAL;
694 : : }
695 : :
696 [ + + ]: 16610 : if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) !=
697 : : SPDK_BLOB_INVALID_FLAGS_MASK) {
698 : 24 : return -EINVAL;
699 : : }
700 : :
701 [ + + ]: 16586 : if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) !=
702 : : SPDK_BLOB_DATA_RO_FLAGS_MASK) {
703 : 36 : blob->data_ro = true;
704 : 36 : blob->md_ro = true;
705 : : }
706 : :
707 [ + + ]: 16586 : if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) !=
708 : : SPDK_BLOB_MD_RO_FLAGS_MASK) {
709 : 36 : blob->md_ro = true;
710 : : }
711 : :
712 [ + + ]: 16586 : if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
713 : 1801 : blob->data_ro = true;
714 : 1801 : blob->md_ro = true;
715 : : }
716 : :
717 : 16586 : blob->invalid_flags = desc_flags->invalid_flags;
718 : 16586 : blob->data_ro_flags = desc_flags->data_ro_flags;
719 : 16586 : blob->md_ro_flags = desc_flags->md_ro_flags;
720 : :
721 [ + + ]: 39696 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
722 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
723 : : unsigned int i, j;
724 : 4176 : unsigned int cluster_count = blob->active.num_clusters;
725 : :
726 [ - + - + ]: 4176 : if (blob->extent_table_found) {
727 : : /* Extent Table already present in the md,
728 : : * both descriptors should never be at the same time. */
729 : 0 : return -EINVAL;
730 : : }
731 : 4176 : blob->extent_rle_found = true;
732 : :
733 : 4176 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
734 : :
735 [ + - ]: 4176 : if (desc_extent_rle->length == 0 ||
736 [ - + ]: 4176 : (desc_extent_rle->length % sizeof(desc_extent_rle->extents[0]) != 0)) {
737 : 0 : return -EINVAL;
738 : : }
739 : :
740 [ + + ]: 8886 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
741 [ + + ]: 63714 : for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
742 [ + + ]: 59004 : if (desc_extent_rle->extents[i].cluster_idx != 0) {
743 [ - + ]: 20076 : if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters,
744 : 20076 : desc_extent_rle->extents[i].cluster_idx + j)) {
745 : 0 : return -EINVAL;
746 : : }
747 : : }
748 : 59004 : cluster_count++;
749 : : }
750 : : }
751 : :
752 [ - + ]: 4176 : if (cluster_count == 0) {
753 : 0 : return -EINVAL;
754 : : }
755 : 4176 : tmp = realloc(blob->active.clusters, cluster_count * sizeof(*blob->active.clusters));
756 [ - + ]: 4176 : if (tmp == NULL) {
757 : 0 : return -ENOMEM;
758 : : }
759 : 4176 : blob->active.clusters = tmp;
760 : 4176 : blob->active.cluster_array_size = cluster_count;
761 : :
762 [ + + ]: 8886 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
763 [ + + ]: 63714 : for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
764 [ + + ]: 59004 : if (desc_extent_rle->extents[i].cluster_idx != 0) {
765 : 26768 : blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
766 : 20076 : desc_extent_rle->extents[i].cluster_idx + j);
767 : 20076 : blob->active.num_allocated_clusters++;
768 [ + - ]: 38928 : } else if (spdk_blob_is_thin_provisioned(blob)) {
769 : 38928 : blob->active.clusters[blob->active.num_clusters++] = 0;
770 : : } else {
771 : 0 : return -EINVAL;
772 : : }
773 : : }
774 : : }
775 [ + + ]: 35520 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
776 : : struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
777 : 11600 : uint32_t num_extent_pages = blob->active.num_extent_pages;
778 : : uint32_t i, j;
779 : : size_t extent_pages_length;
780 : :
781 : 11600 : desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
782 : 11600 : extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
783 : :
784 [ - + - + ]: 11600 : if (blob->extent_rle_found) {
785 : : /* This means that Extent RLE is present in MD,
786 : : * both should never be at the same time. */
787 : 0 : return -EINVAL;
788 [ - + - + ]: 11600 : } else if (blob->extent_table_found &&
789 [ # # ]: 0 : desc_extent_table->num_clusters != blob->remaining_clusters_in_et) {
790 : : /* Number of clusters in this ET does not match number
791 : : * from previously read EXTENT_TABLE. */
792 : 0 : return -EINVAL;
793 : : }
794 : :
795 [ + - ]: 11600 : if (desc_extent_table->length == 0 ||
796 [ - + ]: 11600 : (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
797 : 0 : return -EINVAL;
798 : : }
799 : :
800 : 11600 : blob->extent_table_found = true;
801 : :
802 [ + + ]: 22272 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
803 : 10672 : num_extent_pages += desc_extent_table->extent_page[i].num_pages;
804 : : }
805 : :
806 [ + + ]: 11600 : if (num_extent_pages > 0) {
807 : 9518 : tmp = realloc(blob->active.extent_pages, num_extent_pages * sizeof(uint32_t));
808 [ - + ]: 9518 : if (tmp == NULL) {
809 : 0 : return -ENOMEM;
810 : : }
811 : 9518 : blob->active.extent_pages = tmp;
812 : : }
813 : 11600 : blob->active.extent_pages_array_size = num_extent_pages;
814 : :
815 : 11600 : blob->remaining_clusters_in_et = desc_extent_table->num_clusters;
816 : :
817 : : /* Extent table entries contain md page numbers for extent pages.
818 : : * Zeroes represent unallocated extent pages, those are run-length-encoded.
819 : : */
820 [ + + ]: 22272 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
821 [ + + ]: 10672 : if (desc_extent_table->extent_page[i].page_idx != 0) {
822 [ - + ]: 9032 : assert(desc_extent_table->extent_page[i].num_pages == 1);
823 : 16993 : blob->active.extent_pages[blob->active.num_extent_pages++] =
824 : 9032 : desc_extent_table->extent_page[i].page_idx;
825 [ + - ]: 1640 : } else if (spdk_blob_is_thin_provisioned(blob)) {
826 [ + + ]: 7544 : for (j = 0; j < desc_extent_table->extent_page[i].num_pages; j++) {
827 : 5904 : blob->active.extent_pages[blob->active.num_extent_pages++] = 0;
828 : : }
829 : : } else {
830 : 0 : return -EINVAL;
831 : : }
832 : : }
833 [ + + ]: 23920 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
834 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
835 : : unsigned int i;
836 : 9014 : unsigned int cluster_count = 0;
837 : : size_t cluster_idx_length;
838 : :
839 [ - + - + ]: 9014 : if (blob->extent_rle_found) {
840 : : /* This means that Extent RLE is present in MD,
841 : : * both should never be at the same time. */
842 : 0 : return -EINVAL;
843 : : }
844 : :
845 : 9014 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
846 : 9014 : cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
847 : :
848 [ + - ]: 9014 : if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
849 [ - + ]: 9014 : (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
850 : 0 : return -EINVAL;
851 : : }
852 : :
853 [ + + ]: 970621 : for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
854 [ + + ]: 961607 : if (desc_extent->cluster_idx[i] != 0) {
855 [ - + ]: 884181 : if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters, desc_extent->cluster_idx[i])) {
856 : 0 : return -EINVAL;
857 : : }
858 : : }
859 : 961607 : cluster_count++;
860 : : }
861 : :
862 [ - + ]: 9014 : if (cluster_count == 0) {
863 : 0 : return -EINVAL;
864 : : }
865 : :
866 : : /* When reading extent pages sequentially starting cluster idx should match
867 : : * current size of a blob.
868 : : * If changed to batch reading, this check shall be removed. */
869 [ - + ]: 9014 : if (desc_extent->start_cluster_idx != blob->active.num_clusters) {
870 : 0 : return -EINVAL;
871 : : }
872 : :
873 : 9014 : tmp = realloc(blob->active.clusters,
874 : 9014 : (cluster_count + blob->active.num_clusters) * sizeof(*blob->active.clusters));
875 [ - + ]: 9014 : if (tmp == NULL) {
876 : 0 : return -ENOMEM;
877 : : }
878 : 9014 : blob->active.clusters = tmp;
879 : 9014 : blob->active.cluster_array_size = (cluster_count + blob->active.num_clusters);
880 : :
881 [ + + ]: 970621 : for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
882 [ + + ]: 961607 : if (desc_extent->cluster_idx[i] != 0) {
883 : 884181 : blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
884 : : desc_extent->cluster_idx[i]);
885 : 884181 : blob->active.num_allocated_clusters++;
886 [ + - ]: 77426 : } else if (spdk_blob_is_thin_provisioned(blob)) {
887 : 77426 : blob->active.clusters[blob->active.num_clusters++] = 0;
888 : : } else {
889 : 0 : return -EINVAL;
890 : : }
891 : : }
892 [ - + ]: 9014 : assert(desc_extent->start_cluster_idx + cluster_count == blob->active.num_clusters);
893 [ - + ]: 9014 : assert(blob->remaining_clusters_in_et >= cluster_count);
894 : 9014 : blob->remaining_clusters_in_et -= cluster_count;
895 [ + + ]: 14906 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
896 : : int rc;
897 : :
898 : 12005 : rc = blob_deserialize_xattr(blob,
899 : : (struct spdk_blob_md_descriptor_xattr *) desc, false);
900 [ - + ]: 12005 : if (rc != 0) {
901 : 0 : return rc;
902 : : }
903 [ + - ]: 2901 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
904 : : int rc;
905 : :
906 : 2901 : rc = blob_deserialize_xattr(blob,
907 : : (struct spdk_blob_md_descriptor_xattr *) desc, true);
908 [ - + ]: 2901 : if (rc != 0) {
909 : 0 : return rc;
910 : : }
911 : : } else {
912 : : /* Unrecognized descriptor type. Do not fail - just continue to the
913 : : * next descriptor. If this descriptor is associated with some feature
914 : : * defined in a newer version of blobstore, that version of blobstore
915 : : * should create and set an associated feature flag to specify if this
916 : : * blob can be loaded or not.
917 : : */
918 : : }
919 : :
920 : : /* Advance to the next descriptor */
921 : 56282 : cur_desc += sizeof(*desc) + desc->length;
922 [ + + ]: 56282 : if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
923 : 120 : break;
924 : : }
925 : 56162 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
926 : : }
927 : :
928 : 25900 : return 0;
929 : : }
930 : :
931 : : static bool bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page);
932 : :
933 : : static int
934 : 9014 : blob_parse_extent_page(struct spdk_blob_md_page *extent_page, struct spdk_blob *blob)
935 : : {
936 [ - + ]: 9014 : assert(blob != NULL);
937 [ - + ]: 9014 : assert(blob->state == SPDK_BLOB_STATE_LOADING);
938 : :
939 [ - + ]: 9014 : if (bs_load_cur_extent_page_valid(extent_page) == false) {
940 : 0 : return -ENOENT;
941 : : }
942 : :
943 : 9014 : return blob_parse_page(extent_page, blob);
944 : : }
945 : :
946 : : static int
947 : 16622 : blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count,
948 : : struct spdk_blob *blob)
949 : : {
950 : : const struct spdk_blob_md_page *page;
951 : : uint32_t i;
952 : : int rc;
953 : : void *tmp;
954 : :
955 [ - + ]: 16622 : assert(page_count > 0);
956 [ - + ]: 16622 : assert(pages[0].sequence_num == 0);
957 [ - + ]: 16622 : assert(blob != NULL);
958 [ - + ]: 16622 : assert(blob->state == SPDK_BLOB_STATE_LOADING);
959 [ - + ]: 16622 : assert(blob->active.clusters == NULL);
960 : :
961 : : /* The blobid provided doesn't match what's in the MD, this can
962 : : * happen for example if a bogus blobid is passed in through open.
963 : : */
964 [ + + ]: 16622 : if (blob->id != pages[0].id) {
965 : 12 : SPDK_ERRLOG("Blobid (0x%" PRIx64 ") doesn't match what's in metadata "
966 : : "(0x%" PRIx64 ")\n", blob->id, pages[0].id);
967 : 12 : return -ENOENT;
968 : : }
969 : :
970 : 16610 : tmp = realloc(blob->active.pages, page_count * sizeof(*blob->active.pages));
971 [ - + ]: 16610 : if (!tmp) {
972 : 0 : return -ENOMEM;
973 : : }
974 : 16610 : blob->active.pages = tmp;
975 : :
976 : 16610 : blob->active.pages[0] = pages[0].id;
977 : :
978 [ + + ]: 16910 : for (i = 1; i < page_count; i++) {
979 [ - + ]: 300 : assert(spdk_bit_array_get(blob->bs->used_md_pages, pages[i - 1].next));
980 : 300 : blob->active.pages[i] = pages[i - 1].next;
981 : : }
982 : 16610 : blob->active.num_pages = page_count;
983 : :
984 [ + + ]: 33496 : for (i = 0; i < page_count; i++) {
985 : 16910 : page = &pages[i];
986 : :
987 [ - + ]: 16910 : assert(page->id == blob->id);
988 [ - + ]: 16910 : assert(page->sequence_num == i);
989 : :
990 : 16910 : rc = blob_parse_page(page, blob);
991 [ + + ]: 16910 : if (rc != 0) {
992 : 24 : return rc;
993 : : }
994 : : }
995 : :
996 : 16586 : return 0;
997 : : }
998 : :
999 : : static int
1000 : 80475 : blob_serialize_add_page(const struct spdk_blob *blob,
1001 : : struct spdk_blob_md_page **pages,
1002 : : uint32_t *page_count,
1003 : : struct spdk_blob_md_page **last_page)
1004 : : {
1005 : : struct spdk_blob_md_page *page, *tmp_pages;
1006 : :
1007 [ - + ]: 80475 : assert(pages != NULL);
1008 [ - + ]: 80475 : assert(page_count != NULL);
1009 : :
1010 : 80475 : *last_page = NULL;
1011 [ + + ]: 80475 : if (*page_count == 0) {
1012 [ - + ]: 80211 : assert(*pages == NULL);
1013 : 80211 : *pages = spdk_malloc(SPDK_BS_PAGE_SIZE, 0,
1014 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
1015 [ - + ]: 80211 : if (*pages == NULL) {
1016 : 0 : return -ENOMEM;
1017 : : }
1018 : 80211 : *page_count = 1;
1019 : : } else {
1020 [ - + ]: 264 : assert(*pages != NULL);
1021 : 264 : tmp_pages = spdk_realloc(*pages, SPDK_BS_PAGE_SIZE * (*page_count + 1), 0);
1022 [ - + ]: 264 : if (tmp_pages == NULL) {
1023 : 0 : return -ENOMEM;
1024 : : }
1025 : 264 : (*page_count)++;
1026 : 264 : *pages = tmp_pages;
1027 : : }
1028 : :
1029 : 80475 : page = &(*pages)[*page_count - 1];
1030 [ - + ]: 80475 : memset(page, 0, sizeof(*page));
1031 : 80475 : page->id = blob->id;
1032 : 80475 : page->sequence_num = *page_count - 1;
1033 : 80475 : page->next = SPDK_INVALID_MD_PAGE;
1034 : 80475 : *last_page = page;
1035 : :
1036 : 80475 : return 0;
1037 : : }
1038 : :
1039 : : /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor.
1040 : : * Update required_sz on both success and failure.
1041 : : *
1042 : : */
1043 : : static int
1044 : 83963 : blob_serialize_xattr(const struct spdk_xattr *xattr,
1045 : : uint8_t *buf, size_t buf_sz,
1046 : : size_t *required_sz, bool internal)
1047 : : {
1048 : : struct spdk_blob_md_descriptor_xattr *desc;
1049 : :
1050 : 166109 : *required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) +
1051 [ - + ]: 83963 : strlen(xattr->name) +
1052 : 83963 : xattr->value_len;
1053 : :
1054 [ + + ]: 83963 : if (buf_sz < *required_sz) {
1055 : 144 : return -1;
1056 : : }
1057 : :
1058 : 83819 : desc = (struct spdk_blob_md_descriptor_xattr *)buf;
1059 : :
1060 [ + + ]: 83819 : desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR;
1061 : 83819 : desc->length = sizeof(desc->name_length) +
1062 : : sizeof(desc->value_length) +
1063 [ - + ]: 83819 : strlen(xattr->name) +
1064 : 83819 : xattr->value_len;
1065 [ - + ]: 83819 : desc->name_length = strlen(xattr->name);
1066 : 83819 : desc->value_length = xattr->value_len;
1067 : :
1068 [ - + - + ]: 83819 : memcpy(desc->name, xattr->name, desc->name_length);
1069 [ - + - + ]: 83819 : memcpy((void *)((uintptr_t)desc->name + desc->name_length),
1070 : 83819 : xattr->value,
1071 : 83819 : desc->value_length);
1072 : :
1073 : 83819 : return 0;
1074 : : }
1075 : :
1076 : : static void
1077 : 45145 : blob_serialize_extent_table_entry(const struct spdk_blob *blob,
1078 : : uint64_t start_ep, uint64_t *next_ep,
1079 : : uint8_t **buf, size_t *remaining_sz)
1080 : : {
1081 : : struct spdk_blob_md_descriptor_extent_table *desc;
1082 : : size_t cur_sz;
1083 : : uint64_t i, et_idx;
1084 : : uint32_t extent_page, ep_len;
1085 : :
1086 : : /* The buffer must have room for at least num_clusters entry */
1087 : 45145 : cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->num_clusters);
1088 [ + + ]: 45145 : if (*remaining_sz < cur_sz) {
1089 : 60 : *next_ep = start_ep;
1090 : 60 : return;
1091 : : }
1092 : :
1093 : 45085 : desc = (struct spdk_blob_md_descriptor_extent_table *)*buf;
1094 : 45085 : desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE;
1095 : :
1096 : 45085 : desc->num_clusters = blob->active.num_clusters;
1097 : :
1098 : 45085 : ep_len = 1;
1099 : 45085 : et_idx = 0;
1100 [ + + ]: 93919 : for (i = start_ep; i < blob->active.num_extent_pages; i++) {
1101 [ - + ]: 48834 : if (*remaining_sz < cur_sz + sizeof(desc->extent_page[0])) {
1102 : : /* If we ran out of buffer space, return */
1103 : 0 : break;
1104 : : }
1105 : :
1106 : 48834 : extent_page = blob->active.extent_pages[i];
1107 : : /* Verify that next extent_page is unallocated */
1108 [ + + ]: 48834 : if (extent_page == 0 &&
1109 [ + + + + ]: 6293 : (i + 1 < blob->active.num_extent_pages && blob->active.extent_pages[i + 1] == 0)) {
1110 : 4784 : ep_len++;
1111 : 4784 : continue;
1112 : : }
1113 : 44050 : desc->extent_page[et_idx].page_idx = extent_page;
1114 : 44050 : desc->extent_page[et_idx].num_pages = ep_len;
1115 : 44050 : et_idx++;
1116 : :
1117 : 44050 : ep_len = 1;
1118 : 44050 : cur_sz += sizeof(desc->extent_page[et_idx]);
1119 : : }
1120 : 45085 : *next_ep = i;
1121 : :
1122 : 45085 : desc->length = sizeof(desc->num_clusters) + sizeof(desc->extent_page[0]) * et_idx;
1123 : 45085 : *remaining_sz -= sizeof(struct spdk_blob_md_descriptor) + desc->length;
1124 : 45085 : *buf += sizeof(struct spdk_blob_md_descriptor) + desc->length;
1125 : : }
1126 : :
1127 : : static int
1128 : 45091 : blob_serialize_extent_table(const struct spdk_blob *blob,
1129 : : struct spdk_blob_md_page **pages,
1130 : : struct spdk_blob_md_page *cur_page,
1131 : : uint32_t *page_count, uint8_t **buf,
1132 : : size_t *remaining_sz)
1133 : : {
1134 : 44867 : uint64_t last_extent_page;
1135 : : int rc;
1136 : :
1137 : 45091 : last_extent_page = 0;
1138 : : /* At least single extent table entry has to be always persisted.
1139 : : * Such case occurs with num_extent_pages == 0. */
1140 [ + - ]: 45145 : while (last_extent_page <= blob->active.num_extent_pages) {
1141 : 45145 : blob_serialize_extent_table_entry(blob, last_extent_page, &last_extent_page, buf,
1142 : : remaining_sz);
1143 : :
1144 [ + + ]: 45145 : if (last_extent_page == blob->active.num_extent_pages) {
1145 : 45091 : break;
1146 : : }
1147 : :
1148 : 54 : rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
1149 [ - + ]: 54 : if (rc < 0) {
1150 : 0 : return rc;
1151 : : }
1152 : :
1153 : 54 : *buf = (uint8_t *)cur_page->descriptors;
1154 : 54 : *remaining_sz = sizeof(cur_page->descriptors);
1155 : : }
1156 : :
1157 : 45091 : return 0;
1158 : : }
1159 : :
1160 : : static void
1161 : 5211 : blob_serialize_extent_rle(const struct spdk_blob *blob,
1162 : : uint64_t start_cluster, uint64_t *next_cluster,
1163 : : uint8_t **buf, size_t *buf_sz)
1164 : : {
1165 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
1166 : : size_t cur_sz;
1167 : : uint64_t i, extent_idx;
1168 : : uint64_t lba, lba_per_cluster, lba_count;
1169 : :
1170 : : /* The buffer must have room for at least one extent */
1171 : 5211 : cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc_extent_rle->extents[0]);
1172 [ + + ]: 5211 : if (*buf_sz < cur_sz) {
1173 : 54 : *next_cluster = start_cluster;
1174 : 54 : return;
1175 : : }
1176 : :
1177 : 5157 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)*buf;
1178 : 5157 : desc_extent_rle->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE;
1179 : :
1180 : 5157 : lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
1181 : : /* Assert for scan-build false positive */
1182 [ - + ]: 5157 : assert(lba_per_cluster > 0);
1183 : :
1184 : 5157 : lba = blob->active.clusters[start_cluster];
1185 : 5157 : lba_count = lba_per_cluster;
1186 : 5157 : extent_idx = 0;
1187 [ + + ]: 2431350 : for (i = start_cluster + 1; i < blob->active.num_clusters; i++) {
1188 [ + + + + ]: 2426205 : if ((lba + lba_count) == blob->active.clusters[i] && lba != 0) {
1189 : : /* Run-length encode sequential non-zero LBA */
1190 : 21828 : lba_count += lba_per_cluster;
1191 : 21828 : continue;
1192 [ + + + + ]: 2404377 : } else if (lba == 0 && blob->active.clusters[i] == 0) {
1193 : : /* Run-length encode unallocated clusters */
1194 : 2400798 : lba_count += lba_per_cluster;
1195 : 2400798 : continue;
1196 : : }
1197 [ - + ]: 3579 : desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
1198 [ - + ]: 3579 : desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
1199 : 3579 : extent_idx++;
1200 : :
1201 : 3579 : cur_sz += sizeof(desc_extent_rle->extents[extent_idx]);
1202 : :
1203 [ + + ]: 3579 : if (*buf_sz < cur_sz) {
1204 : : /* If we ran out of buffer space, return */
1205 : 12 : *next_cluster = i;
1206 : 12 : break;
1207 : : }
1208 : :
1209 : 3567 : lba = blob->active.clusters[i];
1210 : 3567 : lba_count = lba_per_cluster;
1211 : : }
1212 : :
1213 [ + + ]: 5157 : if (*buf_sz >= cur_sz) {
1214 [ - + ]: 5145 : desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
1215 [ - + ]: 5145 : desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
1216 : 5145 : extent_idx++;
1217 : :
1218 : 5145 : *next_cluster = blob->active.num_clusters;
1219 : : }
1220 : :
1221 : 5157 : desc_extent_rle->length = sizeof(desc_extent_rle->extents[0]) * extent_idx;
1222 : 5157 : *buf_sz -= sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
1223 : 5157 : *buf += sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
1224 : : }
1225 : :
1226 : : static int
1227 : 5787 : blob_serialize_extents_rle(const struct spdk_blob *blob,
1228 : : struct spdk_blob_md_page **pages,
1229 : : struct spdk_blob_md_page *cur_page,
1230 : : uint32_t *page_count, uint8_t **buf,
1231 : : size_t *remaining_sz)
1232 : : {
1233 : 5787 : uint64_t last_cluster;
1234 : : int rc;
1235 : :
1236 : 5787 : last_cluster = 0;
1237 [ + + ]: 5853 : while (last_cluster < blob->active.num_clusters) {
1238 : 5211 : blob_serialize_extent_rle(blob, last_cluster, &last_cluster, buf, remaining_sz);
1239 : :
1240 [ + + ]: 5211 : if (last_cluster == blob->active.num_clusters) {
1241 : 5145 : break;
1242 : : }
1243 : :
1244 : 66 : rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
1245 [ - + ]: 66 : if (rc < 0) {
1246 : 0 : return rc;
1247 : : }
1248 : :
1249 : 66 : *buf = (uint8_t *)cur_page->descriptors;
1250 : 66 : *remaining_sz = sizeof(cur_page->descriptors);
1251 : : }
1252 : :
1253 : 5787 : return 0;
1254 : : }
1255 : :
1256 : : static void
1257 : 32409 : blob_serialize_extent_page(const struct spdk_blob *blob,
1258 : : uint64_t cluster, struct spdk_blob_md_page *page)
1259 : : {
1260 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
1261 : : uint64_t i, extent_idx;
1262 : : uint64_t lba, lba_per_cluster;
1263 [ - + ]: 32409 : uint64_t start_cluster_idx = (cluster / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
1264 : :
1265 : 32409 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *) page->descriptors;
1266 : 32409 : desc_extent->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE;
1267 : :
1268 : 32409 : lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
1269 : :
1270 : 32409 : desc_extent->start_cluster_idx = start_cluster_idx;
1271 : 32409 : extent_idx = 0;
1272 [ + + ]: 2362514 : for (i = start_cluster_idx; i < blob->active.num_clusters; i++) {
1273 : 2332200 : lba = blob->active.clusters[i];
1274 [ - + ]: 2332200 : desc_extent->cluster_idx[extent_idx++] = lba / lba_per_cluster;
1275 [ + + ]: 2332200 : if (extent_idx >= SPDK_EXTENTS_PER_EP) {
1276 : 2095 : break;
1277 : : }
1278 : : }
1279 : 32409 : desc_extent->length = sizeof(desc_extent->start_cluster_idx) +
1280 : : sizeof(desc_extent->cluster_idx[0]) * extent_idx;
1281 : 32409 : }
1282 : :
1283 : : static void
1284 : 50878 : blob_serialize_flags(const struct spdk_blob *blob,
1285 : : uint8_t *buf, size_t *buf_sz)
1286 : : {
1287 : : struct spdk_blob_md_descriptor_flags *desc;
1288 : :
1289 : : /*
1290 : : * Flags get serialized first, so we should always have room for the flags
1291 : : * descriptor.
1292 : : */
1293 [ - + ]: 50878 : assert(*buf_sz >= sizeof(*desc));
1294 : :
1295 : 50878 : desc = (struct spdk_blob_md_descriptor_flags *)buf;
1296 : 50878 : desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS;
1297 : 50878 : desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor);
1298 : 50878 : desc->invalid_flags = blob->invalid_flags;
1299 : 50878 : desc->data_ro_flags = blob->data_ro_flags;
1300 : 50878 : desc->md_ro_flags = blob->md_ro_flags;
1301 : :
1302 : 50878 : *buf_sz -= sizeof(*desc);
1303 : 50878 : }
1304 : :
1305 : : static int
1306 : 101756 : blob_serialize_xattrs(const struct spdk_blob *blob,
1307 : : const struct spdk_xattr_tailq *xattrs, bool internal,
1308 : : struct spdk_blob_md_page **pages,
1309 : : struct spdk_blob_md_page *cur_page,
1310 : : uint32_t *page_count, uint8_t **buf,
1311 : : size_t *remaining_sz)
1312 : : {
1313 : : const struct spdk_xattr *xattr;
1314 : : int rc;
1315 : :
1316 [ + + ]: 185575 : TAILQ_FOREACH(xattr, xattrs, link) {
1317 : 83819 : size_t required_sz = 0;
1318 : :
1319 : 83819 : rc = blob_serialize_xattr(xattr,
1320 : : *buf, *remaining_sz,
1321 : : &required_sz, internal);
1322 [ + + ]: 83819 : if (rc < 0) {
1323 : : /* Need to add a new page to the chain */
1324 : 144 : rc = blob_serialize_add_page(blob, pages, page_count,
1325 : : &cur_page);
1326 [ - + ]: 144 : if (rc < 0) {
1327 : 0 : spdk_free(*pages);
1328 : 0 : *pages = NULL;
1329 : 0 : *page_count = 0;
1330 : 0 : return rc;
1331 : : }
1332 : :
1333 : 144 : *buf = (uint8_t *)cur_page->descriptors;
1334 : 144 : *remaining_sz = sizeof(cur_page->descriptors);
1335 : :
1336 : : /* Try again */
1337 : 144 : required_sz = 0;
1338 : 144 : rc = blob_serialize_xattr(xattr,
1339 : : *buf, *remaining_sz,
1340 : : &required_sz, internal);
1341 : :
1342 [ - + ]: 144 : if (rc < 0) {
1343 : 0 : spdk_free(*pages);
1344 : 0 : *pages = NULL;
1345 : 0 : *page_count = 0;
1346 : 0 : return rc;
1347 : : }
1348 : : }
1349 : :
1350 : 83819 : *remaining_sz -= required_sz;
1351 : 83819 : *buf += required_sz;
1352 : : }
1353 : :
1354 : 101756 : return 0;
1355 : : }
1356 : :
1357 : : static int
1358 : 50878 : blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages,
1359 : : uint32_t *page_count)
1360 : : {
1361 : 50654 : struct spdk_blob_md_page *cur_page;
1362 : : int rc;
1363 : 50654 : uint8_t *buf;
1364 : 50654 : size_t remaining_sz;
1365 : :
1366 [ - + ]: 50878 : assert(pages != NULL);
1367 [ - + ]: 50878 : assert(page_count != NULL);
1368 [ - + ]: 50878 : assert(blob != NULL);
1369 [ - + ]: 50878 : assert(blob->state == SPDK_BLOB_STATE_DIRTY);
1370 : :
1371 : 50878 : *pages = NULL;
1372 : 50878 : *page_count = 0;
1373 : :
1374 : : /* A blob always has at least 1 page, even if it has no descriptors */
1375 : 50878 : rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
1376 [ - + ]: 50878 : if (rc < 0) {
1377 : 0 : return rc;
1378 : : }
1379 : :
1380 : 50878 : buf = (uint8_t *)cur_page->descriptors;
1381 : 50878 : remaining_sz = sizeof(cur_page->descriptors);
1382 : :
1383 : : /* Serialize flags */
1384 : 50878 : blob_serialize_flags(blob, buf, &remaining_sz);
1385 : 50878 : buf += sizeof(struct spdk_blob_md_descriptor_flags);
1386 : :
1387 : : /* Serialize xattrs */
1388 : 50878 : rc = blob_serialize_xattrs(blob, &blob->xattrs, false,
1389 : : pages, cur_page, page_count, &buf, &remaining_sz);
1390 [ - + ]: 50878 : if (rc < 0) {
1391 : 0 : return rc;
1392 : : }
1393 : :
1394 : : /* Serialize internal xattrs */
1395 : 50878 : rc = blob_serialize_xattrs(blob, &blob->xattrs_internal, true,
1396 : : pages, cur_page, page_count, &buf, &remaining_sz);
1397 [ - + ]: 50878 : if (rc < 0) {
1398 : 0 : return rc;
1399 : : }
1400 : :
1401 [ + + + + ]: 50878 : if (blob->use_extent_table) {
1402 : : /* Serialize extent table */
1403 : 45091 : rc = blob_serialize_extent_table(blob, pages, cur_page, page_count, &buf, &remaining_sz);
1404 : : } else {
1405 : : /* Serialize extents */
1406 : 5787 : rc = blob_serialize_extents_rle(blob, pages, cur_page, page_count, &buf, &remaining_sz);
1407 : : }
1408 : :
1409 : 50878 : return rc;
1410 : : }
1411 : :
1412 : : struct spdk_blob_load_ctx {
1413 : : struct spdk_blob *blob;
1414 : :
1415 : : struct spdk_blob_md_page *pages;
1416 : : uint32_t num_pages;
1417 : : uint32_t next_extent_page;
1418 : : spdk_bs_sequence_t *seq;
1419 : :
1420 : : spdk_bs_sequence_cpl cb_fn;
1421 : : void *cb_arg;
1422 : : };
1423 : :
1424 : : static uint32_t
1425 : 149832 : blob_md_page_calc_crc(void *page)
1426 : : {
1427 : : uint32_t crc;
1428 : :
1429 : 149832 : crc = BLOB_CRC32C_INITIAL;
1430 : 149832 : crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc);
1431 : 149832 : crc ^= BLOB_CRC32C_INITIAL;
1432 : :
1433 : 149832 : return crc;
1434 : :
1435 : : }
1436 : :
1437 : : static void
1438 : 16706 : blob_load_final(struct spdk_blob_load_ctx *ctx, int bserrno)
1439 : : {
1440 : 16706 : struct spdk_blob *blob = ctx->blob;
1441 : :
1442 [ + + ]: 16706 : if (bserrno == 0) {
1443 : 16514 : blob_mark_clean(blob);
1444 : : }
1445 : :
1446 : 16706 : ctx->cb_fn(ctx->seq, ctx->cb_arg, bserrno);
1447 : :
1448 : : /* Free the memory */
1449 : 16706 : spdk_free(ctx->pages);
1450 : 16706 : free(ctx);
1451 : 16706 : }
1452 : :
1453 : : static void
1454 : 1500 : blob_load_snapshot_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
1455 : : {
1456 : 1500 : struct spdk_blob_load_ctx *ctx = cb_arg;
1457 : 1500 : struct spdk_blob *blob = ctx->blob;
1458 : :
1459 [ + + ]: 1500 : if (bserrno == 0) {
1460 : 1482 : blob->back_bs_dev = bs_create_blob_bs_dev(snapshot);
1461 [ - + ]: 1482 : if (blob->back_bs_dev == NULL) {
1462 : 0 : bserrno = -ENOMEM;
1463 : : }
1464 : : }
1465 [ + + ]: 1500 : if (bserrno != 0) {
1466 : 18 : SPDK_ERRLOG("Snapshot fail\n");
1467 : : }
1468 : :
1469 : 1500 : blob_load_final(ctx, bserrno);
1470 : 1500 : }
1471 : :
1472 : : static void blob_update_clear_method(struct spdk_blob *blob);
1473 : :
1474 : : static int
1475 : 437 : blob_load_esnap(struct spdk_blob *blob, void *blob_ctx)
1476 : : {
1477 : 437 : struct spdk_blob_store *bs = blob->bs;
1478 : 437 : struct spdk_bs_dev *bs_dev = NULL;
1479 : 437 : const void *esnap_id = NULL;
1480 : 437 : size_t id_len = 0;
1481 : : int rc;
1482 : :
1483 [ + + ]: 437 : if (bs->esnap_bs_dev_create == NULL) {
1484 : 24 : SPDK_NOTICELOG("blob 0x%" PRIx64 " is an esnap clone but the blobstore was opened "
1485 : : "without support for esnap clones\n", blob->id);
1486 : 24 : return -ENOTSUP;
1487 : : }
1488 [ - + ]: 413 : assert(blob->back_bs_dev == NULL);
1489 : :
1490 : 413 : rc = blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, &esnap_id, &id_len, true);
1491 [ - + ]: 413 : if (rc != 0) {
1492 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " is an esnap clone but has no esnap ID\n", blob->id);
1493 : 0 : return -EINVAL;
1494 : : }
1495 [ + - + - ]: 413 : assert(id_len > 0 && id_len < UINT32_MAX);
1496 : :
1497 [ - + - + ]: 413 : SPDK_INFOLOG(blob, "Creating external snapshot device\n");
1498 : :
1499 : 413 : rc = bs->esnap_bs_dev_create(bs->esnap_ctx, blob_ctx, blob, esnap_id, (uint32_t)id_len,
1500 : : &bs_dev);
1501 [ - + ]: 413 : if (rc != 0) {
1502 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": failed to load back_bs_dev "
1503 : : "with error %d\n", blob->id, rc);
1504 : 0 : return rc;
1505 : : }
1506 : :
1507 : : /*
1508 : : * Note: bs_dev might be NULL if the consumer chose to not open the external snapshot.
1509 : : * This especially might happen during spdk_bs_load() iteration.
1510 : : */
1511 [ + + ]: 413 : if (bs_dev != NULL) {
1512 [ - + - + ]: 369 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": loaded back_bs_dev\n", blob->id);
1513 [ + + + + ]: 369 : if ((bs->io_unit_size % bs_dev->blocklen) != 0) {
1514 : 12 : SPDK_NOTICELOG("blob 0x%" PRIx64 " external snapshot device block size %u "
1515 : : "is not compatible with blobstore block size %u\n",
1516 : : blob->id, bs_dev->blocklen, bs->io_unit_size);
1517 : 12 : bs_dev->destroy(bs_dev);
1518 : 12 : return -EINVAL;
1519 : : }
1520 : : }
1521 : :
1522 : 401 : blob->back_bs_dev = bs_dev;
1523 : 401 : blob->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
1524 : :
1525 : 401 : return 0;
1526 : : }
1527 : :
1528 : : static void
1529 : 16568 : blob_load_backing_dev(spdk_bs_sequence_t *seq, void *cb_arg)
1530 : : {
1531 : 16568 : struct spdk_blob_load_ctx *ctx = cb_arg;
1532 : 16568 : struct spdk_blob *blob = ctx->blob;
1533 : 16220 : const void *value;
1534 : 16220 : size_t len;
1535 : : int rc;
1536 : :
1537 [ + + ]: 16568 : if (blob_is_esnap_clone(blob)) {
1538 : 437 : rc = blob_load_esnap(blob, seq->cpl.u.blob_handle.esnap_ctx);
1539 : 437 : blob_load_final(ctx, rc);
1540 : 459 : return;
1541 : : }
1542 : :
1543 [ + + ]: 16131 : if (spdk_blob_is_thin_provisioned(blob)) {
1544 : 3437 : rc = blob_get_xattr_value(blob, BLOB_SNAPSHOT, &value, &len, true);
1545 [ + + ]: 3437 : if (rc == 0) {
1546 [ - + ]: 1500 : if (len != sizeof(spdk_blob_id)) {
1547 : 0 : blob_load_final(ctx, -EINVAL);
1548 : 0 : return;
1549 : : }
1550 : : /* open snapshot blob and continue in the callback function */
1551 : 1500 : blob->parent_id = *(spdk_blob_id *)value;
1552 : 1500 : spdk_bs_open_blob(blob->bs, blob->parent_id,
1553 : : blob_load_snapshot_cpl, ctx);
1554 : 1500 : return;
1555 : : } else {
1556 : : /* add zeroes_dev for thin provisioned blob */
1557 : 1937 : blob->back_bs_dev = bs_create_zeroes_dev();
1558 : : }
1559 : : } else {
1560 : : /* standard blob */
1561 : 12694 : blob->back_bs_dev = NULL;
1562 : : }
1563 : 14631 : blob_load_final(ctx, 0);
1564 : : }
1565 : :
1566 : : static void
1567 : 20632 : blob_load_cpl_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1568 : : {
1569 : 20632 : struct spdk_blob_load_ctx *ctx = cb_arg;
1570 : 20632 : struct spdk_blob *blob = ctx->blob;
1571 : : struct spdk_blob_md_page *page;
1572 : : uint64_t i;
1573 : : uint32_t crc;
1574 : : uint64_t lba;
1575 : : void *tmp;
1576 : : uint64_t sz;
1577 : :
1578 [ + + ]: 20632 : if (bserrno) {
1579 : 18 : SPDK_ERRLOG("Extent page read failed: %d\n", bserrno);
1580 : 18 : blob_load_final(ctx, bserrno);
1581 : 18 : return;
1582 : : }
1583 : :
1584 [ + + ]: 20614 : if (ctx->pages == NULL) {
1585 : : /* First iteration of this function, allocate buffer for single EXTENT_PAGE */
1586 : 11600 : ctx->pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
1587 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
1588 [ - + ]: 11600 : if (!ctx->pages) {
1589 : 0 : blob_load_final(ctx, -ENOMEM);
1590 : 0 : return;
1591 : : }
1592 : 11600 : ctx->num_pages = 1;
1593 : 11600 : ctx->next_extent_page = 0;
1594 : : } else {
1595 : 9014 : page = &ctx->pages[0];
1596 : 9014 : crc = blob_md_page_calc_crc(page);
1597 [ - + ]: 9014 : if (crc != page->crc) {
1598 : 0 : blob_load_final(ctx, -EINVAL);
1599 : 0 : return;
1600 : : }
1601 : :
1602 [ - + ]: 9014 : if (page->next != SPDK_INVALID_MD_PAGE) {
1603 : 0 : blob_load_final(ctx, -EINVAL);
1604 : 0 : return;
1605 : : }
1606 : :
1607 : 9014 : bserrno = blob_parse_extent_page(page, blob);
1608 [ - + ]: 9014 : if (bserrno) {
1609 : 0 : blob_load_final(ctx, bserrno);
1610 : 0 : return;
1611 : : }
1612 : : }
1613 : :
1614 [ + + ]: 26518 : for (i = ctx->next_extent_page; i < blob->active.num_extent_pages; i++) {
1615 [ + + ]: 14936 : if (blob->active.extent_pages[i] != 0) {
1616 : : /* Extent page was allocated, read and parse it. */
1617 : 9032 : lba = bs_md_page_to_lba(blob->bs, blob->active.extent_pages[i]);
1618 : 9032 : ctx->next_extent_page = i + 1;
1619 : :
1620 : 9032 : bs_sequence_read_dev(seq, &ctx->pages[0], lba,
1621 : 9032 : bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
1622 : : blob_load_cpl_extents_cpl, ctx);
1623 : 9032 : return;
1624 : : } else {
1625 : : /* Thin provisioned blobs can point to unallocated extent pages.
1626 : : * In this case blob size should be increased by up to the amount left in remaining_clusters_in_et. */
1627 : :
1628 [ + + ]: 5904 : sz = spdk_min(blob->remaining_clusters_in_et, SPDK_EXTENTS_PER_EP);
1629 : 5904 : blob->active.num_clusters += sz;
1630 : 5904 : blob->remaining_clusters_in_et -= sz;
1631 : :
1632 [ - + ]: 5904 : assert(spdk_blob_is_thin_provisioned(blob));
1633 [ + + - + ]: 5904 : assert(i + 1 < blob->active.num_extent_pages || blob->remaining_clusters_in_et == 0);
1634 : :
1635 : 5904 : tmp = realloc(blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
1636 [ - + ]: 5904 : if (tmp == NULL) {
1637 : 0 : blob_load_final(ctx, -ENOMEM);
1638 : 0 : return;
1639 : : }
1640 [ - + ]: 5904 : memset(tmp + sizeof(*blob->active.clusters) * blob->active.cluster_array_size, 0,
1641 : 5904 : sizeof(*blob->active.clusters) * (blob->active.num_clusters - blob->active.cluster_array_size));
1642 : 5904 : blob->active.clusters = tmp;
1643 : 5904 : blob->active.cluster_array_size = blob->active.num_clusters;
1644 : : }
1645 : : }
1646 : :
1647 : 11582 : blob_load_backing_dev(seq, ctx);
1648 : : }
1649 : :
1650 : : static void
1651 : 17006 : blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1652 : : {
1653 : 17006 : struct spdk_blob_load_ctx *ctx = cb_arg;
1654 : 17006 : struct spdk_blob *blob = ctx->blob;
1655 : : struct spdk_blob_md_page *page;
1656 : : int rc;
1657 : : uint32_t crc;
1658 : : uint32_t current_page;
1659 : :
1660 [ + + ]: 17006 : if (ctx->num_pages == 1) {
1661 : 16706 : current_page = bs_blobid_to_page(blob->id);
1662 : : } else {
1663 [ - + ]: 300 : assert(ctx->num_pages != 0);
1664 : 300 : page = &ctx->pages[ctx->num_pages - 2];
1665 : 300 : current_page = page->next;
1666 : : }
1667 : :
1668 [ + + ]: 17006 : if (bserrno) {
1669 : 60 : SPDK_ERRLOG("Metadata page %d read failed for blobid 0x%" PRIx64 ": %d\n",
1670 : : current_page, blob->id, bserrno);
1671 : 60 : blob_load_final(ctx, bserrno);
1672 : 60 : return;
1673 : : }
1674 : :
1675 : 16946 : page = &ctx->pages[ctx->num_pages - 1];
1676 : 16946 : crc = blob_md_page_calc_crc(page);
1677 [ + + ]: 16946 : if (crc != page->crc) {
1678 : 24 : SPDK_ERRLOG("Metadata page %d crc mismatch for blobid 0x%" PRIx64 "\n",
1679 : : current_page, blob->id);
1680 : 24 : blob_load_final(ctx, -EINVAL);
1681 : 24 : return;
1682 : : }
1683 : :
1684 [ + + ]: 16922 : if (page->next != SPDK_INVALID_MD_PAGE) {
1685 : : struct spdk_blob_md_page *tmp_pages;
1686 : 300 : uint32_t next_page = page->next;
1687 : 300 : uint64_t next_lba = bs_md_page_to_lba(blob->bs, next_page);
1688 : :
1689 : : /* Read the next page */
1690 : 300 : tmp_pages = spdk_realloc(ctx->pages, (sizeof(*page) * (ctx->num_pages + 1)), 0);
1691 [ - + ]: 300 : if (tmp_pages == NULL) {
1692 : 0 : blob_load_final(ctx, -ENOMEM);
1693 : 0 : return;
1694 : : }
1695 : 300 : ctx->num_pages++;
1696 : 300 : ctx->pages = tmp_pages;
1697 : :
1698 : 300 : bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1],
1699 : : next_lba,
1700 : 300 : bs_byte_to_lba(blob->bs, sizeof(*page)),
1701 : : blob_load_cpl, ctx);
1702 : 300 : return;
1703 : : }
1704 : :
1705 : : /* Parse the pages */
1706 : 16622 : rc = blob_parse(ctx->pages, ctx->num_pages, blob);
1707 [ + + ]: 16622 : if (rc) {
1708 : 36 : blob_load_final(ctx, rc);
1709 : 36 : return;
1710 : : }
1711 : :
1712 [ + + + + ]: 16586 : if (blob->extent_table_found == true) {
1713 : : /* If EXTENT_TABLE was found, that means support for it should be enabled. */
1714 [ - + - + ]: 11600 : assert(blob->extent_rle_found == false);
1715 : 11600 : blob->use_extent_table = true;
1716 : : } else {
1717 : : /* If EXTENT_RLE or no extent_* descriptor was found disable support
1718 : : * for extent table. No extent_* descriptors means that blob has length of 0
1719 : : * and no extent_rle descriptors were persisted for it.
1720 : : * EXTENT_TABLE if used, is always present in metadata regardless of length. */
1721 : 4986 : blob->use_extent_table = false;
1722 : : }
1723 : :
1724 : : /* Check the clear_method stored in metadata vs what may have been passed
1725 : : * via spdk_bs_open_blob_ext() and update accordingly.
1726 : : */
1727 : 16586 : blob_update_clear_method(blob);
1728 : :
1729 : 16586 : spdk_free(ctx->pages);
1730 : 16586 : ctx->pages = NULL;
1731 : :
1732 [ + + + + ]: 16586 : if (blob->extent_table_found) {
1733 : 11600 : blob_load_cpl_extents_cpl(seq, ctx, 0);
1734 : : } else {
1735 : 4986 : blob_load_backing_dev(seq, ctx);
1736 : : }
1737 : : }
1738 : :
1739 : : /* Load a blob from disk given a blobid */
1740 : : static void
1741 : 16706 : blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
1742 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg)
1743 : : {
1744 : : struct spdk_blob_load_ctx *ctx;
1745 : : struct spdk_blob_store *bs;
1746 : : uint32_t page_num;
1747 : : uint64_t lba;
1748 : :
1749 : 16706 : blob_verify_md_op(blob);
1750 : :
1751 : 16706 : bs = blob->bs;
1752 : :
1753 : 16706 : ctx = calloc(1, sizeof(*ctx));
1754 [ - + ]: 16706 : if (!ctx) {
1755 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
1756 : 0 : return;
1757 : : }
1758 : :
1759 : 16706 : ctx->blob = blob;
1760 : 16706 : ctx->pages = spdk_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, 0);
1761 [ - + ]: 16706 : if (!ctx->pages) {
1762 : 0 : free(ctx);
1763 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
1764 : 0 : return;
1765 : : }
1766 : 16706 : ctx->num_pages = 1;
1767 : 16706 : ctx->cb_fn = cb_fn;
1768 : 16706 : ctx->cb_arg = cb_arg;
1769 : 16706 : ctx->seq = seq;
1770 : :
1771 : 16706 : page_num = bs_blobid_to_page(blob->id);
1772 : 16706 : lba = bs_md_page_to_lba(blob->bs, page_num);
1773 : :
1774 : 16706 : blob->state = SPDK_BLOB_STATE_LOADING;
1775 : :
1776 : 16706 : bs_sequence_read_dev(seq, &ctx->pages[0], lba,
1777 : 16706 : bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE),
1778 : : blob_load_cpl, ctx);
1779 : : }
1780 : :
1781 : : struct spdk_blob_persist_ctx {
1782 : : struct spdk_blob *blob;
1783 : :
1784 : : struct spdk_blob_md_page *pages;
1785 : : uint32_t next_extent_page;
1786 : : struct spdk_blob_md_page *extent_page;
1787 : :
1788 : : spdk_bs_sequence_t *seq;
1789 : : spdk_bs_sequence_cpl cb_fn;
1790 : : void *cb_arg;
1791 : : TAILQ_ENTRY(spdk_blob_persist_ctx) link;
1792 : : };
1793 : :
1794 : : static void
1795 : 5290 : bs_batch_clear_dev(struct spdk_blob *blob, spdk_bs_batch_t *batch, uint64_t lba,
1796 : : uint64_t lba_count)
1797 : : {
1798 [ + - + ]: 5290 : switch (blob->clear_method) {
1799 : 5289 : case BLOB_CLEAR_WITH_DEFAULT:
1800 : : case BLOB_CLEAR_WITH_UNMAP:
1801 : 5289 : bs_batch_unmap_dev(batch, lba, lba_count);
1802 : 5289 : break;
1803 : 0 : case BLOB_CLEAR_WITH_WRITE_ZEROES:
1804 : 0 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
1805 : 0 : break;
1806 : 1 : case BLOB_CLEAR_WITH_NONE:
1807 : : default:
1808 : 1 : break;
1809 : : }
1810 : 5290 : }
1811 : :
1812 : : static int
1813 : 10293 : bs_super_validate(struct spdk_bs_super_block *super, struct spdk_blob_store *bs)
1814 : : {
1815 : : uint32_t crc;
1816 : : static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
1817 : :
1818 [ + + ]: 10293 : if (super->version > SPDK_BS_VERSION ||
1819 [ + + ]: 9649 : super->version < SPDK_BS_INITIAL_VERSION) {
1820 : 6143 : return -EILSEQ;
1821 : : }
1822 : :
1823 [ + + + + ]: 4150 : if (memcmp(super->signature, SPDK_BS_SUPER_BLOCK_SIG,
1824 : : sizeof(super->signature)) != 0) {
1825 : 301 : return -EILSEQ;
1826 : : }
1827 : :
1828 : 3849 : crc = blob_md_page_calc_crc(super);
1829 [ + + ]: 3849 : if (crc != super->crc) {
1830 : 12 : return -EILSEQ;
1831 : : }
1832 : :
1833 [ + + - + : 3837 : if (memcmp(&bs->bstype, &super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
+ + ]
1834 [ - + - + ]: 3777 : SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n");
1835 [ + + + + ]: 60 : } else if (memcmp(&bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
1836 [ - + - + ]: 28 : SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n");
1837 : : } else {
1838 [ - + - + ]: 32 : SPDK_DEBUGLOG(blob, "Unexpected bstype\n");
1839 [ - + - + ]: 32 : SPDK_LOGDUMP(blob, "Expected:", bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1840 [ - + - + ]: 32 : SPDK_LOGDUMP(blob, "Found:", super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1841 : 32 : return -ENXIO;
1842 : : }
1843 : :
1844 [ + + ]: 3805 : if (super->size > bs->dev->blockcnt * bs->dev->blocklen) {
1845 : 24 : SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n",
1846 : : bs->dev->blockcnt * bs->dev->blocklen, super->size);
1847 : 24 : return -EILSEQ;
1848 : : }
1849 : :
1850 : 3781 : return 0;
1851 : : }
1852 : :
1853 : : static void bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
1854 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg);
1855 : :
1856 : : static void
1857 : 56111 : blob_persist_complete_cb(void *arg)
1858 : : {
1859 : 56111 : struct spdk_blob_persist_ctx *ctx = arg;
1860 : :
1861 : : /* Call user callback */
1862 : 56111 : ctx->cb_fn(ctx->seq, ctx->cb_arg, 0);
1863 : :
1864 : : /* Free the memory */
1865 : 56111 : spdk_free(ctx->pages);
1866 : 56111 : free(ctx);
1867 : 56111 : }
1868 : :
1869 : : static void blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
1870 : :
1871 : : static void
1872 : 56111 : blob_persist_complete(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx, int bserrno)
1873 : : {
1874 : : struct spdk_blob_persist_ctx *next_persist, *tmp;
1875 : 56111 : struct spdk_blob *blob = ctx->blob;
1876 : :
1877 [ + + ]: 56111 : if (bserrno == 0) {
1878 : 55955 : blob_mark_clean(blob);
1879 : : }
1880 : :
1881 [ - + ]: 56111 : assert(ctx == TAILQ_FIRST(&blob->persists_to_complete));
1882 : :
1883 : : /* Complete all persists that were pending when the current persist started */
1884 [ + + ]: 112222 : TAILQ_FOREACH_SAFE(next_persist, &blob->persists_to_complete, link, tmp) {
1885 [ - + ]: 56111 : TAILQ_REMOVE(&blob->persists_to_complete, next_persist, link);
1886 : 56111 : spdk_thread_send_msg(spdk_get_thread(), blob_persist_complete_cb, next_persist);
1887 : : }
1888 : :
1889 [ + + ]: 56111 : if (TAILQ_EMPTY(&blob->pending_persists)) {
1890 : 56041 : return;
1891 : : }
1892 : :
1893 : : /* Queue up all pending persists for completion and start blob persist with first one */
1894 [ + - - + ]: 70 : TAILQ_SWAP(&blob->persists_to_complete, &blob->pending_persists, spdk_blob_persist_ctx, link);
1895 : 70 : next_persist = TAILQ_FIRST(&blob->persists_to_complete);
1896 : :
1897 : 70 : blob->state = SPDK_BLOB_STATE_DIRTY;
1898 : 70 : bs_mark_dirty(seq, blob->bs, blob_persist_start, next_persist);
1899 : : }
1900 : :
1901 : : static void
1902 : 55955 : blob_persist_clear_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1903 : : {
1904 : 55955 : struct spdk_blob_persist_ctx *ctx = cb_arg;
1905 : 55955 : struct spdk_blob *blob = ctx->blob;
1906 : 55955 : struct spdk_blob_store *bs = blob->bs;
1907 : : size_t i;
1908 : :
1909 [ - + ]: 55955 : if (bserrno != 0) {
1910 : 0 : blob_persist_complete(seq, ctx, bserrno);
1911 : 0 : return;
1912 : : }
1913 : :
1914 : 55955 : spdk_spin_lock(&bs->used_lock);
1915 : :
1916 : : /* Release all extent_pages that were truncated */
1917 [ + + ]: 62956 : for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
1918 : : /* Nothing to release if it was not allocated */
1919 [ + + ]: 7001 : if (blob->active.extent_pages[i] != 0) {
1920 : 2965 : bs_release_md_page(bs, blob->active.extent_pages[i]);
1921 : : }
1922 : : }
1923 : :
1924 : 55955 : spdk_spin_unlock(&bs->used_lock);
1925 : :
1926 [ + + ]: 55955 : if (blob->active.num_extent_pages == 0) {
1927 : 12679 : free(blob->active.extent_pages);
1928 : 12679 : blob->active.extent_pages = NULL;
1929 : 12679 : blob->active.extent_pages_array_size = 0;
1930 [ + + ]: 43276 : } else if (blob->active.num_extent_pages != blob->active.extent_pages_array_size) {
1931 : : #ifndef __clang_analyzer__
1932 : : void *tmp;
1933 : :
1934 : : /* scan-build really can't figure reallocs, workaround it */
1935 : 6 : tmp = realloc(blob->active.extent_pages, sizeof(uint32_t) * blob->active.num_extent_pages);
1936 [ - + ]: 6 : assert(tmp != NULL);
1937 : 6 : blob->active.extent_pages = tmp;
1938 : : #endif
1939 : 6 : blob->active.extent_pages_array_size = blob->active.num_extent_pages;
1940 : : }
1941 : :
1942 : 55955 : blob_persist_complete(seq, ctx, bserrno);
1943 : : }
1944 : :
1945 : : static void
1946 : 55955 : blob_persist_clear_extents(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
1947 : : {
1948 : 55955 : struct spdk_blob *blob = ctx->blob;
1949 : 55955 : struct spdk_blob_store *bs = blob->bs;
1950 : : size_t i;
1951 : : uint64_t lba;
1952 : : uint64_t lba_count;
1953 : : spdk_bs_batch_t *batch;
1954 : :
1955 : 55955 : batch = bs_sequence_to_batch(seq, blob_persist_clear_extents_cpl, ctx);
1956 : 55955 : lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
1957 : :
1958 : : /* Clear all extent_pages that were truncated */
1959 [ + + ]: 62956 : for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
1960 : : /* Nothing to clear if it was not allocated */
1961 [ + + ]: 7001 : if (blob->active.extent_pages[i] != 0) {
1962 : 2965 : lba = bs_md_page_to_lba(bs, blob->active.extent_pages[i]);
1963 : 2965 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
1964 : : }
1965 : : }
1966 : :
1967 : 55955 : bs_batch_close(batch);
1968 : 55955 : }
1969 : :
1970 : : static void
1971 : 55955 : blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1972 : : {
1973 : 55955 : struct spdk_blob_persist_ctx *ctx = cb_arg;
1974 : 55955 : struct spdk_blob *blob = ctx->blob;
1975 : 55955 : struct spdk_blob_store *bs = blob->bs;
1976 : : size_t i;
1977 : :
1978 [ - + ]: 55955 : if (bserrno != 0) {
1979 : 0 : blob_persist_complete(seq, ctx, bserrno);
1980 : 0 : return;
1981 : : }
1982 : :
1983 : 55955 : spdk_spin_lock(&bs->used_lock);
1984 : : /* Release all clusters that were truncated */
1985 [ + + ]: 3911132 : for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
1986 : 3855184 : uint32_t cluster_num = bs_lba_to_cluster(bs, blob->active.clusters[i]);
1987 : :
1988 : : /* Nothing to release if it was not allocated */
1989 [ + + ]: 3855184 : if (blob->active.clusters[i] != 0) {
1990 : 308229 : bs_release_cluster(bs, cluster_num);
1991 : : }
1992 : : }
1993 : 55955 : spdk_spin_unlock(&bs->used_lock);
1994 : :
1995 [ + + ]: 55955 : if (blob->active.num_clusters == 0) {
1996 : 7588 : free(blob->active.clusters);
1997 : 7588 : blob->active.clusters = NULL;
1998 : 7588 : blob->active.cluster_array_size = 0;
1999 [ + + ]: 48367 : } else if (blob->active.num_clusters != blob->active.cluster_array_size) {
2000 : : #ifndef __clang_analyzer__
2001 : : void *tmp;
2002 : :
2003 : : /* scan-build really can't figure reallocs, workaround it */
2004 : 45 : tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * blob->active.num_clusters);
2005 [ - + ]: 45 : assert(tmp != NULL);
2006 : 45 : blob->active.clusters = tmp;
2007 : :
2008 : : #endif
2009 : 45 : blob->active.cluster_array_size = blob->active.num_clusters;
2010 : : }
2011 : :
2012 : : /* Move on to clearing extent pages */
2013 : 55955 : blob_persist_clear_extents(seq, ctx);
2014 : : }
2015 : :
2016 : : static void
2017 : 55955 : blob_persist_clear_clusters(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
2018 : : {
2019 : 55955 : struct spdk_blob *blob = ctx->blob;
2020 : 55955 : struct spdk_blob_store *bs = blob->bs;
2021 : : spdk_bs_batch_t *batch;
2022 : : size_t i;
2023 : : uint64_t lba;
2024 : : uint64_t lba_count;
2025 : :
2026 : : /* Clusters don't move around in blobs. The list shrinks or grows
2027 : : * at the end, but no changes ever occur in the middle of the list.
2028 : : */
2029 : :
2030 : 55955 : batch = bs_sequence_to_batch(seq, blob_persist_clear_clusters_cpl, ctx);
2031 : :
2032 : : /* Clear all clusters that were truncated */
2033 : 55955 : lba = 0;
2034 : 55955 : lba_count = 0;
2035 [ + + ]: 3911132 : for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
2036 : 3855184 : uint64_t next_lba = blob->active.clusters[i];
2037 : 3855184 : uint64_t next_lba_count = bs_cluster_to_lba(bs, 1);
2038 : :
2039 [ + + + + ]: 3855184 : if (next_lba > 0 && (lba + lba_count) == next_lba) {
2040 : : /* This cluster is contiguous with the previous one. */
2041 : 302959 : lba_count += next_lba_count;
2042 : 302959 : continue;
2043 [ + + ]: 3552223 : } else if (next_lba == 0) {
2044 : 3546947 : continue;
2045 : : }
2046 : :
2047 : : /* This cluster is not contiguous with the previous one. */
2048 : :
2049 : : /* If a run of LBAs previously existing, clear them now */
2050 [ + + ]: 5270 : if (lba_count > 0) {
2051 : 998 : bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
2052 : : }
2053 : :
2054 : : /* Start building the next batch */
2055 : 5270 : lba = next_lba;
2056 [ + - ]: 5270 : if (next_lba > 0) {
2057 : 5270 : lba_count = next_lba_count;
2058 : : } else {
2059 : 0 : lba_count = 0;
2060 : : }
2061 : : }
2062 : :
2063 : : /* If we ended with a contiguous set of LBAs, clear them now */
2064 [ + + ]: 55955 : if (lba_count > 0) {
2065 : 4272 : bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
2066 : : }
2067 : :
2068 : 55955 : bs_batch_close(batch);
2069 : 55955 : }
2070 : :
2071 : : static void
2072 : 55967 : blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2073 : : {
2074 : 55967 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2075 : 55967 : struct spdk_blob *blob = ctx->blob;
2076 : 55967 : struct spdk_blob_store *bs = blob->bs;
2077 : : size_t i;
2078 : :
2079 [ + + ]: 55967 : if (bserrno != 0) {
2080 : 12 : blob_persist_complete(seq, ctx, bserrno);
2081 : 12 : return;
2082 : : }
2083 : :
2084 : 55955 : spdk_spin_lock(&bs->used_lock);
2085 : :
2086 : : /* This loop starts at 1 because the first page is special and handled
2087 : : * below. The pages (except the first) are never written in place,
2088 : : * so any pages in the clean list must be zeroed.
2089 : : */
2090 [ + + ]: 56159 : for (i = 1; i < blob->clean.num_pages; i++) {
2091 : 204 : bs_release_md_page(bs, blob->clean.pages[i]);
2092 : : }
2093 : :
2094 [ + + ]: 55955 : if (blob->active.num_pages == 0) {
2095 : : uint32_t page_num;
2096 : :
2097 : 5197 : page_num = bs_blobid_to_page(blob->id);
2098 : 5197 : bs_release_md_page(bs, page_num);
2099 : : }
2100 : :
2101 : 55955 : spdk_spin_unlock(&bs->used_lock);
2102 : :
2103 : : /* Move on to clearing clusters */
2104 : 55955 : blob_persist_clear_clusters(seq, ctx);
2105 : : }
2106 : :
2107 : : static void
2108 : 56087 : blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2109 : : {
2110 : 56087 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2111 : 56087 : struct spdk_blob *blob = ctx->blob;
2112 : 56087 : struct spdk_blob_store *bs = blob->bs;
2113 : : uint64_t lba;
2114 : : uint64_t lba_count;
2115 : : spdk_bs_batch_t *batch;
2116 : : size_t i;
2117 : :
2118 [ + + ]: 56087 : if (bserrno != 0) {
2119 : 120 : blob_persist_complete(seq, ctx, bserrno);
2120 : 120 : return;
2121 : : }
2122 : :
2123 : 55967 : batch = bs_sequence_to_batch(seq, blob_persist_zero_pages_cpl, ctx);
2124 : :
2125 : 55967 : lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
2126 : :
2127 : : /* This loop starts at 1 because the first page is special and handled
2128 : : * below. The pages (except the first) are never written in place,
2129 : : * so any pages in the clean list must be zeroed.
2130 : : */
2131 [ + + ]: 56171 : for (i = 1; i < blob->clean.num_pages; i++) {
2132 : 204 : lba = bs_md_page_to_lba(bs, blob->clean.pages[i]);
2133 : :
2134 : 204 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
2135 : : }
2136 : :
2137 : : /* The first page will only be zeroed if this is a delete. */
2138 [ + + ]: 55967 : if (blob->active.num_pages == 0) {
2139 : : uint32_t page_num;
2140 : :
2141 : : /* The first page in the metadata goes where the blobid indicates */
2142 : 5209 : page_num = bs_blobid_to_page(blob->id);
2143 : 5209 : lba = bs_md_page_to_lba(bs, page_num);
2144 : :
2145 : 5209 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
2146 : : }
2147 : :
2148 : 55967 : bs_batch_close(batch);
2149 : : }
2150 : :
2151 : : static void
2152 : 50878 : blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2153 : : {
2154 : 50878 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2155 : 50878 : struct spdk_blob *blob = ctx->blob;
2156 : 50878 : struct spdk_blob_store *bs = blob->bs;
2157 : : uint64_t lba;
2158 : : uint32_t lba_count;
2159 : : struct spdk_blob_md_page *page;
2160 : :
2161 [ - + ]: 50878 : if (bserrno != 0) {
2162 : 0 : blob_persist_complete(seq, ctx, bserrno);
2163 : 0 : return;
2164 : : }
2165 : :
2166 [ - + ]: 50878 : if (blob->active.num_pages == 0) {
2167 : : /* Move on to the next step */
2168 : 0 : blob_persist_zero_pages(seq, ctx, 0);
2169 : 0 : return;
2170 : : }
2171 : :
2172 : 50878 : lba_count = bs_byte_to_lba(bs, sizeof(*page));
2173 : :
2174 : 50878 : page = &ctx->pages[0];
2175 : : /* The first page in the metadata goes where the blobid indicates */
2176 : 50878 : lba = bs_md_page_to_lba(bs, bs_blobid_to_page(blob->id));
2177 : :
2178 : 50878 : bs_sequence_write_dev(seq, page, lba, lba_count,
2179 : : blob_persist_zero_pages, ctx);
2180 : : }
2181 : :
2182 : : static void
2183 : 50878 : blob_persist_write_page_chain(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
2184 : : {
2185 : 50878 : struct spdk_blob *blob = ctx->blob;
2186 : 50878 : struct spdk_blob_store *bs = blob->bs;
2187 : : uint64_t lba;
2188 : : uint32_t lba_count;
2189 : : struct spdk_blob_md_page *page;
2190 : : spdk_bs_batch_t *batch;
2191 : : size_t i;
2192 : :
2193 : : /* Clusters don't move around in blobs. The list shrinks or grows
2194 : : * at the end, but no changes ever occur in the middle of the list.
2195 : : */
2196 : :
2197 : 50878 : lba_count = bs_byte_to_lba(bs, sizeof(*page));
2198 : :
2199 : 50878 : batch = bs_sequence_to_batch(seq, blob_persist_write_page_root, ctx);
2200 : :
2201 : : /* This starts at 1. The root page is not written until
2202 : : * all of the others are finished
2203 : : */
2204 [ + + ]: 51142 : for (i = 1; i < blob->active.num_pages; i++) {
2205 : 264 : page = &ctx->pages[i];
2206 [ - + ]: 264 : assert(page->sequence_num == i);
2207 : :
2208 : 264 : lba = bs_md_page_to_lba(bs, blob->active.pages[i]);
2209 : :
2210 : 264 : bs_batch_write_dev(batch, page, lba, lba_count);
2211 : : }
2212 : :
2213 : 50878 : bs_batch_close(batch);
2214 : 50878 : }
2215 : :
2216 : : static int
2217 : 39296 : blob_resize(struct spdk_blob *blob, uint64_t sz)
2218 : : {
2219 : : uint64_t i;
2220 : : uint64_t *tmp;
2221 : 39036 : uint64_t cluster;
2222 : 39036 : uint32_t lfmd; /* lowest free md page */
2223 : : uint64_t num_clusters;
2224 : : uint32_t *ep_tmp;
2225 : 39296 : uint64_t new_num_ep = 0, current_num_ep = 0;
2226 : : struct spdk_blob_store *bs;
2227 : : int rc;
2228 : :
2229 : 39296 : bs = blob->bs;
2230 : :
2231 : 39296 : blob_verify_md_op(blob);
2232 : :
2233 [ + + ]: 39296 : if (blob->active.num_clusters == sz) {
2234 : 2342 : return 0;
2235 : : }
2236 : :
2237 [ - + ]: 36954 : if (blob->active.num_clusters < blob->active.cluster_array_size) {
2238 : : /* If this blob was resized to be larger, then smaller, then
2239 : : * larger without syncing, then the cluster array already
2240 : : * contains spare assigned clusters we can use.
2241 : : */
2242 : 0 : num_clusters = spdk_min(blob->active.cluster_array_size,
2243 : : sz);
2244 : : } else {
2245 : 36954 : num_clusters = blob->active.num_clusters;
2246 : : }
2247 : :
2248 [ + + + + ]: 36954 : if (blob->use_extent_table) {
2249 : : /* Round up since every cluster beyond current Extent Table size,
2250 : : * requires new extent page. */
2251 : 32352 : new_num_ep = spdk_divide_round_up(sz, SPDK_EXTENTS_PER_EP);
2252 : 32352 : current_num_ep = spdk_divide_round_up(num_clusters, SPDK_EXTENTS_PER_EP);
2253 : : }
2254 : :
2255 [ - + ]: 36954 : assert(!spdk_spin_held(&bs->used_lock));
2256 : :
2257 : : /* Check first that we have enough clusters and md pages before we start claiming them.
2258 : : * bs->used_lock is held to ensure that clusters we think are free are still free when we go
2259 : : * to claim them later in this function.
2260 : : */
2261 [ + + + + ]: 36954 : if (sz > num_clusters && spdk_blob_is_thin_provisioned(blob) == false) {
2262 : 30758 : spdk_spin_lock(&bs->used_lock);
2263 [ + + ]: 30758 : if ((sz - num_clusters) > bs->num_free_clusters) {
2264 : 27 : rc = -ENOSPC;
2265 : 27 : goto out;
2266 : : }
2267 : 30731 : lfmd = 0;
2268 [ + + ]: 34067 : for (i = current_num_ep; i < new_num_ep ; i++) {
2269 : 3336 : lfmd = spdk_bit_array_find_first_clear(blob->bs->used_md_pages, lfmd);
2270 [ - + ]: 3336 : if (lfmd == UINT32_MAX) {
2271 : : /* No more free md pages. Cannot satisfy the request */
2272 : 0 : rc = -ENOSPC;
2273 : 0 : goto out;
2274 : : }
2275 : : }
2276 : : }
2277 : :
2278 [ + + ]: 36927 : if (sz > num_clusters) {
2279 : : /* Expand the cluster array if necessary.
2280 : : * We only shrink the array when persisting.
2281 : : */
2282 : 32069 : tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * sz);
2283 [ + - - + ]: 32069 : if (sz > 0 && tmp == NULL) {
2284 : 0 : rc = -ENOMEM;
2285 : 0 : goto out;
2286 : : }
2287 [ - + ]: 32069 : memset(tmp + blob->active.cluster_array_size, 0,
2288 : 32069 : sizeof(*blob->active.clusters) * (sz - blob->active.cluster_array_size));
2289 : 32069 : blob->active.clusters = tmp;
2290 : 32069 : blob->active.cluster_array_size = sz;
2291 : :
2292 : : /* Expand the extents table, only if enough clusters were added */
2293 [ + + + + : 32069 : if (new_num_ep > current_num_ep && blob->use_extent_table) {
+ - ]
2294 : 3559 : ep_tmp = realloc(blob->active.extent_pages, sizeof(*blob->active.extent_pages) * new_num_ep);
2295 [ + - - + ]: 3559 : if (new_num_ep > 0 && ep_tmp == NULL) {
2296 : 0 : rc = -ENOMEM;
2297 : 0 : goto out;
2298 : : }
2299 [ - + ]: 3559 : memset(ep_tmp + blob->active.extent_pages_array_size, 0,
2300 : 3559 : sizeof(*blob->active.extent_pages) * (new_num_ep - blob->active.extent_pages_array_size));
2301 : 3559 : blob->active.extent_pages = ep_tmp;
2302 : 3559 : blob->active.extent_pages_array_size = new_num_ep;
2303 : : }
2304 : : }
2305 : :
2306 : 36927 : blob->state = SPDK_BLOB_STATE_DIRTY;
2307 : :
2308 [ + + ]: 36927 : if (spdk_blob_is_thin_provisioned(blob) == false) {
2309 : 34695 : cluster = 0;
2310 : 34695 : lfmd = 0;
2311 [ + + ]: 386725 : for (i = num_clusters; i < sz; i++) {
2312 : 352030 : bs_allocate_cluster(blob, i, &cluster, &lfmd, true);
2313 : : /* Do not increment lfmd here. lfmd will get updated
2314 : : * to the md_page allocated (if any) when a new extent
2315 : : * page is needed. Just pass that value again,
2316 : : * bs_allocate_cluster will just start at that index
2317 : : * to find the next free md_page when needed.
2318 : : */
2319 : : }
2320 : : }
2321 : :
2322 : : /* If we are shrinking the blob, we must adjust num_allocated_clusters */
2323 [ + + ]: 3892231 : for (i = sz; i < num_clusters; i++) {
2324 [ + + ]: 3855304 : if (blob->active.clusters[i] != 0) {
2325 : 308229 : blob->active.num_allocated_clusters--;
2326 : : }
2327 : : }
2328 : :
2329 : 36927 : blob->active.num_clusters = sz;
2330 : 36927 : blob->active.num_extent_pages = new_num_ep;
2331 : :
2332 : 36927 : rc = 0;
2333 : 36954 : out:
2334 [ + + ]: 36954 : if (spdk_spin_held(&bs->used_lock)) {
2335 : 30758 : spdk_spin_unlock(&bs->used_lock);
2336 : : }
2337 : :
2338 : 36954 : return rc;
2339 : : }
2340 : :
2341 : : static void
2342 : 50878 : blob_persist_generate_new_md(struct spdk_blob_persist_ctx *ctx)
2343 : : {
2344 : 50878 : spdk_bs_sequence_t *seq = ctx->seq;
2345 : 50878 : struct spdk_blob *blob = ctx->blob;
2346 : 50878 : struct spdk_blob_store *bs = blob->bs;
2347 : : uint64_t i;
2348 : : uint32_t page_num;
2349 : : void *tmp;
2350 : : int rc;
2351 : :
2352 : : /* Generate the new metadata */
2353 : 50878 : rc = blob_serialize(blob, &ctx->pages, &blob->active.num_pages);
2354 [ - + ]: 50878 : if (rc < 0) {
2355 : 0 : blob_persist_complete(seq, ctx, rc);
2356 : 0 : return;
2357 : : }
2358 : :
2359 [ - + ]: 50878 : assert(blob->active.num_pages >= 1);
2360 : :
2361 : : /* Resize the cache of page indices */
2362 : 50878 : tmp = realloc(blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
2363 [ - + ]: 50878 : if (!tmp) {
2364 : 0 : blob_persist_complete(seq, ctx, -ENOMEM);
2365 : 0 : return;
2366 : : }
2367 : 50878 : blob->active.pages = tmp;
2368 : :
2369 : : /* Assign this metadata to pages. This requires two passes - one to verify that there are
2370 : : * enough pages and a second to actually claim them. The used_lock is held across
2371 : : * both passes to ensure things don't change in the middle.
2372 : : */
2373 : 50878 : spdk_spin_lock(&bs->used_lock);
2374 : 50878 : page_num = 0;
2375 : : /* Note that this loop starts at one. The first page location is fixed by the blobid. */
2376 [ + + ]: 51142 : for (i = 1; i < blob->active.num_pages; i++) {
2377 : 264 : page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
2378 [ - + ]: 264 : if (page_num == UINT32_MAX) {
2379 : 0 : spdk_spin_unlock(&bs->used_lock);
2380 : 0 : blob_persist_complete(seq, ctx, -ENOMEM);
2381 : 0 : return;
2382 : : }
2383 : 264 : page_num++;
2384 : : }
2385 : :
2386 : 50878 : page_num = 0;
2387 : 50878 : blob->active.pages[0] = bs_blobid_to_page(blob->id);
2388 [ + + ]: 51142 : for (i = 1; i < blob->active.num_pages; i++) {
2389 : 264 : page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
2390 : 264 : ctx->pages[i - 1].next = page_num;
2391 : : /* Now that previous metadata page is complete, calculate the crc for it. */
2392 : 264 : ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
2393 : 264 : blob->active.pages[i] = page_num;
2394 : 264 : bs_claim_md_page(bs, page_num);
2395 [ - + - + ]: 264 : SPDK_DEBUGLOG(blob, "Claiming page %u for blob 0x%" PRIx64 "\n", page_num,
2396 : : blob->id);
2397 : 264 : page_num++;
2398 : : }
2399 : 50878 : spdk_spin_unlock(&bs->used_lock);
2400 : 50878 : ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
2401 : : /* Start writing the metadata from last page to first */
2402 : 50878 : blob->state = SPDK_BLOB_STATE_CLEAN;
2403 : 50878 : blob_persist_write_page_chain(seq, ctx);
2404 : : }
2405 : :
2406 : : static void
2407 : 61365 : blob_persist_write_extent_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2408 : : {
2409 : 61365 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2410 : 61365 : struct spdk_blob *blob = ctx->blob;
2411 : : size_t i;
2412 : : uint32_t extent_page_id;
2413 : 61365 : uint32_t page_count = 0;
2414 : : int rc;
2415 : :
2416 [ + + ]: 61365 : if (ctx->extent_page != NULL) {
2417 : 29333 : spdk_free(ctx->extent_page);
2418 : 29333 : ctx->extent_page = NULL;
2419 : : }
2420 : :
2421 [ - + ]: 61365 : if (bserrno != 0) {
2422 : 0 : blob_persist_complete(seq, ctx, bserrno);
2423 : 90 : return;
2424 : : }
2425 : :
2426 : : /* Only write out Extent Pages when blob was resized. */
2427 [ + + ]: 68913 : for (i = ctx->next_extent_page; i < blob->active.extent_pages_array_size; i++) {
2428 : 36881 : extent_page_id = blob->active.extent_pages[i];
2429 [ + + ]: 36881 : if (extent_page_id == 0) {
2430 : : /* No Extent Page to persist */
2431 [ - + ]: 7548 : assert(spdk_blob_is_thin_provisioned(blob));
2432 : 7548 : continue;
2433 : : }
2434 [ - + ]: 29333 : assert(spdk_bit_array_get(blob->bs->used_md_pages, extent_page_id));
2435 : 29333 : ctx->next_extent_page = i + 1;
2436 : 29333 : rc = blob_serialize_add_page(ctx->blob, &ctx->extent_page, &page_count, &ctx->extent_page);
2437 [ - + ]: 29333 : if (rc < 0) {
2438 : 0 : blob_persist_complete(seq, ctx, rc);
2439 : 0 : return;
2440 : : }
2441 : :
2442 : 29333 : blob->state = SPDK_BLOB_STATE_DIRTY;
2443 : 29333 : blob_serialize_extent_page(blob, i * SPDK_EXTENTS_PER_EP, ctx->extent_page);
2444 : :
2445 : 29333 : ctx->extent_page->crc = blob_md_page_calc_crc(ctx->extent_page);
2446 : :
2447 : 29333 : bs_sequence_write_dev(seq, ctx->extent_page, bs_md_page_to_lba(blob->bs, extent_page_id),
2448 : 29333 : bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
2449 : : blob_persist_write_extent_pages, ctx);
2450 : 29333 : return;
2451 : : }
2452 : :
2453 : 32032 : blob_persist_generate_new_md(ctx);
2454 : : }
2455 : :
2456 : : static void
2457 : 56111 : blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2458 : : {
2459 : 56111 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2460 : 56111 : struct spdk_blob *blob = ctx->blob;
2461 : :
2462 [ + + ]: 56111 : if (bserrno != 0) {
2463 : 24 : blob_persist_complete(seq, ctx, bserrno);
2464 : 24 : return;
2465 : : }
2466 : :
2467 [ + + ]: 56087 : if (blob->active.num_pages == 0) {
2468 : : /* This is the signal that the blob should be deleted.
2469 : : * Immediately jump to the clean up routine. */
2470 [ - + ]: 5209 : assert(blob->clean.num_pages > 0);
2471 : 5209 : blob->state = SPDK_BLOB_STATE_CLEAN;
2472 : 5209 : blob_persist_zero_pages(seq, ctx, 0);
2473 : 5209 : return;
2474 : :
2475 : : }
2476 : :
2477 [ + + ]: 50878 : if (blob->clean.num_clusters < blob->active.num_clusters) {
2478 : : /* Blob was resized up */
2479 [ - + ]: 31985 : assert(blob->clean.num_extent_pages <= blob->active.num_extent_pages);
2480 [ + + ]: 31985 : ctx->next_extent_page = spdk_max(1, blob->clean.num_extent_pages) - 1;
2481 [ + + ]: 18893 : } else if (blob->active.num_clusters < blob->active.cluster_array_size) {
2482 : : /* Blob was resized down */
2483 [ - + ]: 47 : assert(blob->clean.num_extent_pages >= blob->active.num_extent_pages);
2484 [ + + ]: 47 : ctx->next_extent_page = spdk_max(1, blob->active.num_extent_pages) - 1;
2485 : : } else {
2486 : : /* No change in size occurred */
2487 : 18846 : blob_persist_generate_new_md(ctx);
2488 : 18846 : return;
2489 : : }
2490 : :
2491 : 32032 : blob_persist_write_extent_pages(seq, ctx, 0);
2492 : : }
2493 : :
2494 : : struct spdk_bs_mark_dirty {
2495 : : struct spdk_blob_store *bs;
2496 : : struct spdk_bs_super_block *super;
2497 : : spdk_bs_sequence_cpl cb_fn;
2498 : : void *cb_arg;
2499 : : };
2500 : :
2501 : : static void
2502 : 516 : bs_mark_dirty_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2503 : : {
2504 : 516 : struct spdk_bs_mark_dirty *ctx = cb_arg;
2505 : :
2506 [ + + ]: 516 : if (bserrno == 0) {
2507 : 492 : ctx->bs->clean = 0;
2508 : : }
2509 : :
2510 : 516 : ctx->cb_fn(seq, ctx->cb_arg, bserrno);
2511 : :
2512 : 516 : spdk_free(ctx->super);
2513 : 516 : free(ctx);
2514 : 516 : }
2515 : :
2516 : : static void bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
2517 : : struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg);
2518 : :
2519 : :
2520 : : static void
2521 : 516 : bs_mark_dirty_write(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2522 : : {
2523 : 516 : struct spdk_bs_mark_dirty *ctx = cb_arg;
2524 : : int rc;
2525 : :
2526 [ + + ]: 516 : if (bserrno != 0) {
2527 : 12 : bs_mark_dirty_write_cpl(seq, ctx, bserrno);
2528 : 12 : return;
2529 : : }
2530 : :
2531 : 504 : rc = bs_super_validate(ctx->super, ctx->bs);
2532 [ - + ]: 504 : if (rc != 0) {
2533 : 0 : bs_mark_dirty_write_cpl(seq, ctx, rc);
2534 : 0 : return;
2535 : : }
2536 : :
2537 : 504 : ctx->super->clean = 0;
2538 [ + + ]: 504 : if (ctx->super->size == 0) {
2539 : 12 : ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
2540 : : }
2541 : :
2542 : 504 : bs_write_super(seq, ctx->bs, ctx->super, bs_mark_dirty_write_cpl, ctx);
2543 : : }
2544 : :
2545 : : static void
2546 : 59187 : bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
2547 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg)
2548 : : {
2549 : : struct spdk_bs_mark_dirty *ctx;
2550 : :
2551 : : /* Blobstore is already marked dirty */
2552 [ + + + + ]: 59187 : if (bs->clean == 0) {
2553 : 58671 : cb_fn(seq, cb_arg, 0);
2554 : 58671 : return;
2555 : : }
2556 : :
2557 : 516 : ctx = calloc(1, sizeof(*ctx));
2558 [ - + ]: 516 : if (!ctx) {
2559 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
2560 : 0 : return;
2561 : : }
2562 : 516 : ctx->bs = bs;
2563 : 516 : ctx->cb_fn = cb_fn;
2564 : 516 : ctx->cb_arg = cb_arg;
2565 : :
2566 : 516 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
2567 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
2568 [ - + ]: 516 : if (!ctx->super) {
2569 : 0 : free(ctx);
2570 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
2571 : 0 : return;
2572 : : }
2573 : :
2574 : 516 : bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
2575 : 516 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
2576 : : bs_mark_dirty_write, ctx);
2577 : : }
2578 : :
2579 : : /* Write a blob to disk */
2580 : : static void
2581 : 73932 : blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
2582 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg)
2583 : : {
2584 : : struct spdk_blob_persist_ctx *ctx;
2585 : :
2586 : 73932 : blob_verify_md_op(blob);
2587 : :
2588 [ + + + + ]: 73932 : if (blob->state == SPDK_BLOB_STATE_CLEAN && TAILQ_EMPTY(&blob->persists_to_complete)) {
2589 : 17821 : cb_fn(seq, cb_arg, 0);
2590 : 17821 : return;
2591 : : }
2592 : :
2593 : 56111 : ctx = calloc(1, sizeof(*ctx));
2594 [ - + ]: 56111 : if (!ctx) {
2595 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
2596 : 0 : return;
2597 : : }
2598 : 56111 : ctx->blob = blob;
2599 : 56111 : ctx->seq = seq;
2600 : 56111 : ctx->cb_fn = cb_fn;
2601 : 56111 : ctx->cb_arg = cb_arg;
2602 : :
2603 : : /* Multiple blob persists can affect one another, via blob->state or
2604 : : * blob mutable data changes. To prevent it, queue up the persists. */
2605 [ + + ]: 56111 : if (!TAILQ_EMPTY(&blob->persists_to_complete)) {
2606 : 70 : TAILQ_INSERT_TAIL(&blob->pending_persists, ctx, link);
2607 : 70 : return;
2608 : : }
2609 [ - + ]: 56041 : TAILQ_INSERT_HEAD(&blob->persists_to_complete, ctx, link);
2610 : :
2611 : 56041 : bs_mark_dirty(seq, blob->bs, blob_persist_start, ctx);
2612 : : }
2613 : :
2614 : : struct spdk_blob_copy_cluster_ctx {
2615 : : struct spdk_blob *blob;
2616 : : uint8_t *buf;
2617 : : uint64_t page;
2618 : : uint64_t new_cluster;
2619 : : uint32_t new_extent_page;
2620 : : spdk_bs_sequence_t *seq;
2621 : : struct spdk_blob_md_page *new_cluster_page;
2622 : : };
2623 : :
2624 : : struct spdk_blob_free_cluster_ctx {
2625 : : struct spdk_blob *blob;
2626 : : uint64_t page;
2627 : : struct spdk_blob_md_page *md_page;
2628 : : uint64_t cluster_num;
2629 : : uint32_t extent_page;
2630 : : spdk_bs_sequence_t *seq;
2631 : : };
2632 : :
2633 : : static void
2634 : 4214 : blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno)
2635 : : {
2636 : 4214 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2637 : 4214 : struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq;
2638 : 4087 : TAILQ_HEAD(, spdk_bs_request_set) requests;
2639 : : spdk_bs_user_op_t *op;
2640 : :
2641 : 4214 : TAILQ_INIT(&requests);
2642 [ - + + - ]: 4214 : TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link);
2643 : :
2644 [ + + ]: 19487 : while (!TAILQ_EMPTY(&requests)) {
2645 : 15273 : op = TAILQ_FIRST(&requests);
2646 [ + + ]: 15273 : TAILQ_REMOVE(&requests, op, link);
2647 [ + - ]: 15273 : if (bserrno == 0) {
2648 : 15273 : bs_user_op_execute(op);
2649 : : } else {
2650 : 0 : bs_user_op_abort(op, bserrno);
2651 : : }
2652 : : }
2653 : :
2654 : 4214 : spdk_free(ctx->buf);
2655 : 4214 : free(ctx);
2656 : 4214 : }
2657 : :
2658 : : static void
2659 : 180 : blob_free_cluster_cpl(void *cb_arg, int bserrno)
2660 : : {
2661 : 180 : struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
2662 : 180 : spdk_bs_sequence_t *seq = ctx->seq;
2663 : :
2664 : 180 : bs_sequence_finish(seq, bserrno);
2665 : :
2666 : 180 : free(ctx);
2667 : 180 : }
2668 : :
2669 : : static void
2670 : 20 : blob_insert_cluster_revert(struct spdk_blob_copy_cluster_ctx *ctx)
2671 : : {
2672 : 20 : spdk_spin_lock(&ctx->blob->bs->used_lock);
2673 : 20 : bs_release_cluster(ctx->blob->bs, ctx->new_cluster);
2674 [ + + ]: 20 : if (ctx->new_extent_page != 0) {
2675 : 7 : bs_release_md_page(ctx->blob->bs, ctx->new_extent_page);
2676 : : }
2677 : 20 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
2678 : 20 : }
2679 : :
2680 : : static void
2681 : 20 : blob_insert_cluster_clear_cpl(void *cb_arg, int bserrno)
2682 : : {
2683 : 20 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2684 : :
2685 [ - + ]: 20 : if (bserrno) {
2686 : 0 : SPDK_WARNLOG("Failed to clear cluster: %d\n", bserrno);
2687 : : }
2688 : :
2689 : 20 : blob_insert_cluster_revert(ctx);
2690 : 20 : bs_sequence_finish(ctx->seq, bserrno);
2691 : 20 : }
2692 : :
2693 : : static void
2694 : 20 : blob_insert_cluster_clear(struct spdk_blob_copy_cluster_ctx *ctx)
2695 : : {
2696 : 12 : struct spdk_bs_cpl cpl;
2697 : : spdk_bs_batch_t *batch;
2698 : 20 : struct spdk_io_channel *ch = spdk_io_channel_from_ctx(ctx->seq->channel);
2699 : :
2700 : : /*
2701 : : * We allocated a cluster and we copied data to it. But now, we realized that we don't need
2702 : : * this cluster and we want to release it. We must ensure that we clear the data on this
2703 : : * cluster.
2704 : : * The cluster may later be re-allocated by a thick-provisioned blob for example. When
2705 : : * reading from this thick-provisioned blob before writing data, we should read zeroes.
2706 : : */
2707 : :
2708 : 20 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2709 : 20 : cpl.u.blob_basic.cb_fn = blob_insert_cluster_clear_cpl;
2710 : 20 : cpl.u.blob_basic.cb_arg = ctx;
2711 : :
2712 : 20 : batch = bs_batch_open(ch, &cpl, ctx->blob);
2713 [ - + ]: 20 : if (!batch) {
2714 : 0 : blob_insert_cluster_clear_cpl(ctx, -ENOMEM);
2715 : 0 : return;
2716 : : }
2717 : :
2718 : 20 : bs_batch_clear_dev(ctx->blob, batch, bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
2719 : 20 : bs_cluster_to_lba(ctx->blob->bs, 1));
2720 : 20 : bs_batch_close(batch);
2721 : : }
2722 : :
2723 : : static void
2724 : 4214 : blob_insert_cluster_cpl(void *cb_arg, int bserrno)
2725 : : {
2726 : 4214 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2727 : :
2728 [ + + ]: 4214 : if (bserrno) {
2729 [ + - ]: 20 : if (bserrno == -EEXIST) {
2730 : : /* The metadata insert failed because another thread
2731 : : * allocated the cluster first. Clear and free our cluster
2732 : : * but continue without error. */
2733 : 20 : blob_insert_cluster_clear(ctx);
2734 : 20 : return;
2735 : : }
2736 : :
2737 : 0 : blob_insert_cluster_revert(ctx);
2738 : : }
2739 : :
2740 : 4194 : bs_sequence_finish(ctx->seq, bserrno);
2741 : : }
2742 : :
2743 : : static void
2744 : 1333 : blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2745 : : {
2746 : 1333 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2747 : : uint32_t cluster_number;
2748 : :
2749 [ - + ]: 1333 : if (bserrno) {
2750 : : /* The write failed, so jump to the final completion handler */
2751 : 0 : bs_sequence_finish(seq, bserrno);
2752 : 0 : return;
2753 : : }
2754 : :
2755 : 1333 : cluster_number = bs_page_to_cluster(ctx->blob->bs, ctx->page);
2756 : :
2757 : 1333 : blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
2758 : : ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
2759 : : }
2760 : :
2761 : : static void
2762 : 895 : blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2763 : : {
2764 : 895 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2765 : :
2766 [ - + ]: 895 : if (bserrno != 0) {
2767 : : /* The read failed, so jump to the final completion handler */
2768 : 0 : bs_sequence_finish(seq, bserrno);
2769 : 0 : return;
2770 : : }
2771 : :
2772 : : /* Write whole cluster */
2773 : 895 : bs_sequence_write_dev(seq, ctx->buf,
2774 : 895 : bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
2775 : 895 : bs_cluster_to_lba(ctx->blob->bs, 1),
2776 : : blob_write_copy_cpl, ctx);
2777 : : }
2778 : :
2779 : : static bool
2780 : 4167 : blob_can_copy(struct spdk_blob *blob, uint32_t cluster_start_page, uint64_t *base_lba)
2781 : : {
2782 : 4167 : uint64_t lba = bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page);
2783 : :
2784 [ + + + + : 6629 : return (!blob_is_esnap_clone(blob) && blob->bs->dev->copy != NULL) &&
+ + ]
2785 : 2462 : blob->back_bs_dev->translate_lba(blob->back_bs_dev, lba, base_lba);
2786 : : }
2787 : :
2788 : : static void
2789 : 438 : blob_copy(struct spdk_blob_copy_cluster_ctx *ctx, spdk_bs_user_op_t *op, uint64_t src_lba)
2790 : : {
2791 : 438 : struct spdk_blob *blob = ctx->blob;
2792 : 438 : uint64_t lba_count = bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz);
2793 : :
2794 : 438 : bs_sequence_copy_dev(ctx->seq,
2795 : 438 : bs_cluster_to_lba(blob->bs, ctx->new_cluster),
2796 : : src_lba,
2797 : : lba_count,
2798 : : blob_write_copy_cpl, ctx);
2799 : 438 : }
2800 : :
2801 : : static void
2802 : 15274 : bs_allocate_and_copy_cluster(struct spdk_blob *blob,
2803 : : struct spdk_io_channel *_ch,
2804 : : uint64_t io_unit, spdk_bs_user_op_t *op)
2805 : : {
2806 : 12015 : struct spdk_bs_cpl cpl;
2807 : : struct spdk_bs_channel *ch;
2808 : : struct spdk_blob_copy_cluster_ctx *ctx;
2809 : : uint32_t cluster_start_page;
2810 : : uint32_t cluster_number;
2811 : : bool is_zeroes;
2812 : : bool can_copy;
2813 : : bool is_valid_range;
2814 : 12015 : uint64_t copy_src_lba;
2815 : : int rc;
2816 : :
2817 : 15274 : ch = spdk_io_channel_get_ctx(_ch);
2818 : :
2819 [ + + ]: 15274 : if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) {
2820 : : /* There are already operations pending. Queue this user op
2821 : : * and return because it will be re-executed when the outstanding
2822 : : * cluster allocation completes. */
2823 : 11059 : TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
2824 : 11059 : return;
2825 : : }
2826 : :
2827 : : /* Round the io_unit offset down to the first page in the cluster */
2828 : 4215 : cluster_start_page = bs_io_unit_to_cluster_start(blob, io_unit);
2829 : :
2830 : : /* Calculate which index in the metadata cluster array the corresponding
2831 : : * cluster is supposed to be at. */
2832 : 4215 : cluster_number = bs_io_unit_to_cluster_number(blob, io_unit);
2833 : :
2834 : 4215 : ctx = calloc(1, sizeof(*ctx));
2835 [ - + ]: 4215 : if (!ctx) {
2836 : 0 : bs_user_op_abort(op, -ENOMEM);
2837 : 0 : return;
2838 : : }
2839 : :
2840 [ - + - + ]: 4215 : assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0);
2841 : :
2842 : 4215 : ctx->blob = blob;
2843 : 4215 : ctx->page = cluster_start_page;
2844 : 4215 : ctx->new_cluster_page = ch->new_cluster_page;
2845 [ - + ]: 4215 : memset(ctx->new_cluster_page, 0, SPDK_BS_PAGE_SIZE);
2846 : :
2847 : : /* Check if the cluster that we intend to do CoW for is valid for
2848 : : * the backing dev. For zeroes backing dev, it'll be always valid.
2849 : : * For other backing dev e.g. a snapshot, it could be invalid if
2850 : : * the blob has been resized after snapshot was taken. */
2851 : 7615 : is_valid_range = blob->back_bs_dev->is_range_valid(blob->back_bs_dev,
2852 : : bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
2853 : 4215 : bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
2854 : :
2855 [ + + + + ]: 4215 : can_copy = is_valid_range && blob_can_copy(blob, cluster_start_page, ©_src_lba);
2856 : :
2857 [ + + + + ]: 8382 : is_zeroes = is_valid_range && blob->back_bs_dev->is_zeroes(blob->back_bs_dev,
2858 : : bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
2859 : 4167 : bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
2860 [ + + + + : 4215 : if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes && !can_copy) {
+ + ]
2861 : 895 : ctx->buf = spdk_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen,
2862 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
2863 [ - + ]: 895 : if (!ctx->buf) {
2864 : 0 : SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n",
2865 : : blob->bs->cluster_sz);
2866 : 0 : free(ctx);
2867 : 0 : bs_user_op_abort(op, -ENOMEM);
2868 : 0 : return;
2869 : : }
2870 : : }
2871 : :
2872 : 4215 : spdk_spin_lock(&blob->bs->used_lock);
2873 : 4215 : rc = bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, &ctx->new_extent_page,
2874 : : false);
2875 : 4215 : spdk_spin_unlock(&blob->bs->used_lock);
2876 [ + + ]: 4215 : if (rc != 0) {
2877 : 1 : spdk_free(ctx->buf);
2878 : 1 : free(ctx);
2879 : 1 : bs_user_op_abort(op, rc);
2880 : 1 : return;
2881 : : }
2882 : :
2883 : 4214 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2884 : 4214 : cpl.u.blob_basic.cb_fn = blob_allocate_and_copy_cluster_cpl;
2885 : 4214 : cpl.u.blob_basic.cb_arg = ctx;
2886 : :
2887 : 4214 : ctx->seq = bs_sequence_start_blob(_ch, &cpl, blob);
2888 [ - + ]: 4214 : if (!ctx->seq) {
2889 : 0 : spdk_spin_lock(&blob->bs->used_lock);
2890 : 0 : bs_release_cluster(blob->bs, ctx->new_cluster);
2891 : 0 : spdk_spin_unlock(&blob->bs->used_lock);
2892 : 0 : spdk_free(ctx->buf);
2893 : 0 : free(ctx);
2894 : 0 : bs_user_op_abort(op, -ENOMEM);
2895 : 0 : return;
2896 : : }
2897 : :
2898 : : /* Queue the user op to block other incoming operations */
2899 : 4214 : TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
2900 : :
2901 [ + + + + ]: 4214 : if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes) {
2902 [ + + ]: 1333 : if (can_copy) {
2903 : 438 : blob_copy(ctx, op, copy_src_lba);
2904 : : } else {
2905 : : /* Read cluster from backing device */
2906 : 895 : bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf,
2907 : : bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
2908 : 895 : bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz),
2909 : : blob_write_copy, ctx);
2910 : : }
2911 : :
2912 : : } else {
2913 : 2881 : blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
2914 : : ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
2915 : : }
2916 : : }
2917 : :
2918 : : static inline bool
2919 : 7432164 : blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length,
2920 : : uint64_t *lba, uint64_t *lba_count)
2921 : : {
2922 : 7432164 : *lba_count = length;
2923 : :
2924 [ + + ]: 7432164 : if (!bs_io_unit_is_allocated(blob, io_unit)) {
2925 [ - + ]: 1344272 : assert(blob->back_bs_dev != NULL);
2926 : 1344272 : *lba = bs_io_unit_to_back_dev_lba(blob, io_unit);
2927 : 1344272 : *lba_count = bs_io_unit_to_back_dev_lba(blob, *lba_count);
2928 : 1344272 : return false;
2929 : : } else {
2930 : 6087892 : *lba = bs_blob_io_unit_to_lba(blob, io_unit);
2931 : 6087892 : return true;
2932 : : }
2933 : : }
2934 : :
2935 : : struct op_split_ctx {
2936 : : struct spdk_blob *blob;
2937 : : struct spdk_io_channel *channel;
2938 : : uint64_t io_unit_offset;
2939 : : uint64_t io_units_remaining;
2940 : : void *curr_payload;
2941 : : enum spdk_blob_op_type op_type;
2942 : : spdk_bs_sequence_t *seq;
2943 : : bool in_submit_ctx;
2944 : : bool completed_in_submit_ctx;
2945 : : bool done;
2946 : : };
2947 : :
2948 : : static void
2949 : 9237 : blob_request_submit_op_split_next(void *cb_arg, int bserrno)
2950 : : {
2951 : 9237 : struct op_split_ctx *ctx = cb_arg;
2952 : 9237 : struct spdk_blob *blob = ctx->blob;
2953 : 9237 : struct spdk_io_channel *ch = ctx->channel;
2954 : 9237 : enum spdk_blob_op_type op_type = ctx->op_type;
2955 : : uint8_t *buf;
2956 : : uint64_t offset;
2957 : : uint64_t length;
2958 : : uint64_t op_length;
2959 : :
2960 [ + - + + ]: 9237 : if (bserrno != 0 || ctx->io_units_remaining == 0) {
2961 : 568 : bs_sequence_finish(ctx->seq, bserrno);
2962 [ + + + + ]: 568 : if (ctx->in_submit_ctx) {
2963 : : /* Defer freeing of the ctx object, since it will be
2964 : : * accessed when this unwinds back to the submisison
2965 : : * context.
2966 : : */
2967 : 120 : ctx->done = true;
2968 : : } else {
2969 : 448 : free(ctx);
2970 : : }
2971 : 568 : return;
2972 : : }
2973 : :
2974 [ + + + + ]: 8669 : if (ctx->in_submit_ctx) {
2975 : : /* If this split operation completed in the context
2976 : : * of its submission, mark the flag and return immediately
2977 : : * to avoid recursion.
2978 : : */
2979 : 204 : ctx->completed_in_submit_ctx = true;
2980 : 204 : return;
2981 : : }
2982 : :
2983 : : while (true) {
2984 : 8669 : ctx->completed_in_submit_ctx = false;
2985 : :
2986 : 8669 : offset = ctx->io_unit_offset;
2987 : 8669 : length = ctx->io_units_remaining;
2988 : 8669 : buf = ctx->curr_payload;
2989 [ + + ]: 8669 : op_length = spdk_min(length, bs_num_io_units_to_cluster_boundary(blob,
2990 : : offset));
2991 : :
2992 : : /* Update length and payload for next operation */
2993 : 8669 : ctx->io_units_remaining -= op_length;
2994 : 8669 : ctx->io_unit_offset += op_length;
2995 [ + + + + ]: 8669 : if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) {
2996 : 1616 : ctx->curr_payload += op_length * blob->bs->io_unit_size;
2997 : : }
2998 : :
2999 [ - + - + ]: 8669 : assert(!ctx->in_submit_ctx);
3000 : 8669 : ctx->in_submit_ctx = true;
3001 : :
3002 [ + + + + : 8669 : switch (op_type) {
- - ]
3003 : 1270 : case SPDK_BLOB_READ:
3004 : 1270 : spdk_blob_io_read(blob, ch, buf, offset, op_length,
3005 : : blob_request_submit_op_split_next, ctx);
3006 : 1270 : break;
3007 : 346 : case SPDK_BLOB_WRITE:
3008 : 346 : spdk_blob_io_write(blob, ch, buf, offset, op_length,
3009 : : blob_request_submit_op_split_next, ctx);
3010 : 346 : break;
3011 : 6957 : case SPDK_BLOB_UNMAP:
3012 : 6957 : spdk_blob_io_unmap(blob, ch, offset, op_length,
3013 : : blob_request_submit_op_split_next, ctx);
3014 : 6957 : break;
3015 : 96 : case SPDK_BLOB_WRITE_ZEROES:
3016 : 96 : spdk_blob_io_write_zeroes(blob, ch, offset, op_length,
3017 : : blob_request_submit_op_split_next, ctx);
3018 : 96 : break;
3019 : 0 : case SPDK_BLOB_READV:
3020 : : case SPDK_BLOB_WRITEV:
3021 : 0 : SPDK_ERRLOG("readv/write not valid\n");
3022 : 0 : bs_sequence_finish(ctx->seq, -EINVAL);
3023 : 0 : free(ctx);
3024 : 0 : return;
3025 : : }
3026 : :
3027 : : #ifndef __clang_analyzer__
3028 : : /* scan-build reports a false positive around accessing the ctx here. It
3029 : : * forms a path that recursively calls this function, but then says
3030 : : * "assuming ctx->in_submit_ctx is false", when that isn't possible.
3031 : : * This path does free(ctx), returns to here, and reports a use-after-free
3032 : : * bug. Wrapping this bit of code so that scan-build doesn't see it
3033 : : * works around the scan-build bug.
3034 : : */
3035 [ - + - + ]: 8669 : assert(ctx->in_submit_ctx);
3036 : 8669 : ctx->in_submit_ctx = false;
3037 : :
3038 : : /* If the operation completed immediately, loop back and submit the
3039 : : * next operation. Otherwise we can return and the next split
3040 : : * operation will get submitted when this current operation is
3041 : : * later completed asynchronously.
3042 : : */
3043 [ + + + + ]: 8669 : if (ctx->completed_in_submit_ctx) {
3044 : 204 : continue;
3045 [ + + + + ]: 8465 : } else if (ctx->done) {
3046 : 120 : free(ctx);
3047 : : }
3048 : : #endif
3049 : 8465 : break;
3050 : : }
3051 : : }
3052 : :
3053 : : static void
3054 : 568 : blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob,
3055 : : void *payload, uint64_t offset, uint64_t length,
3056 : : spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
3057 : : {
3058 : : struct op_split_ctx *ctx;
3059 : : spdk_bs_sequence_t *seq;
3060 : 560 : struct spdk_bs_cpl cpl;
3061 : :
3062 [ - + ]: 568 : assert(blob != NULL);
3063 : :
3064 : 568 : ctx = calloc(1, sizeof(struct op_split_ctx));
3065 [ - + ]: 568 : if (ctx == NULL) {
3066 : 0 : cb_fn(cb_arg, -ENOMEM);
3067 : 0 : return;
3068 : : }
3069 : :
3070 : 568 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3071 : 568 : cpl.u.blob_basic.cb_fn = cb_fn;
3072 : 568 : cpl.u.blob_basic.cb_arg = cb_arg;
3073 : :
3074 : 568 : seq = bs_sequence_start_blob(ch, &cpl, blob);
3075 [ - + ]: 568 : if (!seq) {
3076 : 0 : free(ctx);
3077 : 0 : cb_fn(cb_arg, -ENOMEM);
3078 : 0 : return;
3079 : : }
3080 : :
3081 : 568 : ctx->blob = blob;
3082 : 568 : ctx->channel = ch;
3083 : 568 : ctx->curr_payload = payload;
3084 : 568 : ctx->io_unit_offset = offset;
3085 : 568 : ctx->io_units_remaining = length;
3086 : 568 : ctx->op_type = op_type;
3087 : 568 : ctx->seq = seq;
3088 : :
3089 : 568 : blob_request_submit_op_split_next(ctx, 0);
3090 : : }
3091 : :
3092 : : static void
3093 : 180 : spdk_free_cluster_unmap_complete(void *cb_arg, int bserrno)
3094 : : {
3095 : 180 : struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
3096 : :
3097 [ - + ]: 180 : if (bserrno) {
3098 : 0 : bs_sequence_finish(ctx->seq, bserrno);
3099 : 0 : free(ctx);
3100 : 0 : return;
3101 : : }
3102 : :
3103 : 180 : blob_free_cluster_on_md_thread(ctx->blob, ctx->cluster_num,
3104 : : ctx->extent_page, ctx->md_page, blob_free_cluster_cpl, ctx);
3105 : : }
3106 : :
3107 : : static void
3108 : 1847561 : blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob,
3109 : : void *payload, uint64_t offset, uint64_t length,
3110 : : spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
3111 : : {
3112 : 1844430 : struct spdk_bs_cpl cpl;
3113 : 1844430 : uint64_t lba;
3114 : 1844430 : uint64_t lba_count;
3115 : : bool is_allocated;
3116 : :
3117 [ - + ]: 1847561 : assert(blob != NULL);
3118 : :
3119 : 1847561 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3120 : 1847561 : cpl.u.blob_basic.cb_fn = cb_fn;
3121 : 1847561 : cpl.u.blob_basic.cb_arg = cb_arg;
3122 : :
3123 [ + + ]: 1847561 : if (blob->frozen_refcnt) {
3124 : : /* This blob I/O is frozen */
3125 : : spdk_bs_user_op_t *op;
3126 : 13 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
3127 : :
3128 : 13 : op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
3129 [ - + ]: 13 : if (!op) {
3130 : 0 : cb_fn(cb_arg, -ENOMEM);
3131 : 0 : return;
3132 : : }
3133 : :
3134 : 13 : TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
3135 : :
3136 : 13 : return;
3137 : : }
3138 : :
3139 : 1847548 : is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
3140 : :
3141 [ + + + - : 1847548 : switch (op_type) {
- ]
3142 : 1402721 : case SPDK_BLOB_READ: {
3143 : : spdk_bs_batch_t *batch;
3144 : :
3145 : 1402721 : batch = bs_batch_open(_ch, &cpl, blob);
3146 [ - + ]: 1402721 : if (!batch) {
3147 : 0 : cb_fn(cb_arg, -ENOMEM);
3148 : 0 : return;
3149 : : }
3150 : :
3151 [ + + ]: 1402721 : if (is_allocated) {
3152 : : /* Read from the blob */
3153 : 1399457 : bs_batch_read_dev(batch, payload, lba, lba_count);
3154 : : } else {
3155 : : /* Read from the backing block device */
3156 : 3264 : bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count);
3157 : : }
3158 : :
3159 : 1402721 : bs_batch_close(batch);
3160 : 1402721 : break;
3161 : : }
3162 : 300109 : case SPDK_BLOB_WRITE:
3163 : : case SPDK_BLOB_WRITE_ZEROES: {
3164 [ + + ]: 300109 : if (is_allocated) {
3165 : : /* Write to the blob */
3166 : : spdk_bs_batch_t *batch;
3167 : :
3168 [ - + ]: 299077 : if (lba_count == 0) {
3169 : 0 : cb_fn(cb_arg, 0);
3170 : 0 : return;
3171 : : }
3172 : :
3173 : 299077 : batch = bs_batch_open(_ch, &cpl, blob);
3174 [ - + ]: 299077 : if (!batch) {
3175 : 0 : cb_fn(cb_arg, -ENOMEM);
3176 : 0 : return;
3177 : : }
3178 : :
3179 [ + + ]: 299077 : if (op_type == SPDK_BLOB_WRITE) {
3180 : 298976 : bs_batch_write_dev(batch, payload, lba, lba_count);
3181 : : } else {
3182 : 101 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
3183 : : }
3184 : :
3185 : 299077 : bs_batch_close(batch);
3186 : : } else {
3187 : : /* Queue this operation and allocate the cluster */
3188 : : spdk_bs_user_op_t *op;
3189 : :
3190 : 1032 : op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
3191 [ - + ]: 1032 : if (!op) {
3192 : 0 : cb_fn(cb_arg, -ENOMEM);
3193 : 0 : return;
3194 : : }
3195 : :
3196 : 1032 : bs_allocate_and_copy_cluster(blob, _ch, offset, op);
3197 : : }
3198 : 300109 : break;
3199 : : }
3200 : 144718 : case SPDK_BLOB_UNMAP: {
3201 : 144718 : struct spdk_blob_free_cluster_ctx *ctx = NULL;
3202 : : spdk_bs_batch_t *batch;
3203 : :
3204 : : /* if aligned with cluster release cluster */
3205 [ + + + - : 144922 : if (spdk_blob_is_thin_provisioned(blob) && is_allocated &&
+ + ]
3206 : 204 : bs_io_units_per_cluster(blob) == length) {
3207 : 180 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
3208 : : uint32_t cluster_start_page;
3209 : : uint32_t cluster_number;
3210 : :
3211 [ - + - + ]: 180 : assert(offset % bs_io_units_per_cluster(blob) == 0);
3212 : :
3213 : : /* Round the io_unit offset down to the first page in the cluster */
3214 : 180 : cluster_start_page = bs_io_unit_to_cluster_start(blob, offset);
3215 : :
3216 : : /* Calculate which index in the metadata cluster array the corresponding
3217 : : * cluster is supposed to be at. */
3218 : 180 : cluster_number = bs_io_unit_to_cluster_number(blob, offset);
3219 : :
3220 : 180 : ctx = calloc(1, sizeof(*ctx));
3221 [ - + ]: 180 : if (!ctx) {
3222 : 0 : cb_fn(cb_arg, -ENOMEM);
3223 : 0 : return;
3224 : : }
3225 : : /* When freeing a cluster the flow should be (in order):
3226 : : * 1. Unmap the underlying area (so if the cluster is reclaimed in the future, it won't leak
3227 : : * old data)
3228 : : * 2. Once the unmap completes (to avoid any races with incoming writes that may claim the
3229 : : * cluster), update and sync metadata freeing the cluster
3230 : : * 3. Once metadata update is done, complete the user unmap request
3231 : : */
3232 : 180 : ctx->blob = blob;
3233 : 180 : ctx->page = cluster_start_page;
3234 : 180 : ctx->cluster_num = cluster_number;
3235 : 180 : ctx->md_page = bs_channel->new_cluster_page;
3236 : 180 : ctx->seq = bs_sequence_start_bs(_ch, &cpl);
3237 [ - + ]: 180 : if (!ctx->seq) {
3238 : 0 : free(ctx);
3239 : 0 : cb_fn(cb_arg, -ENOMEM);
3240 : 0 : return;
3241 : : }
3242 : :
3243 [ + + + + ]: 180 : if (blob->use_extent_table) {
3244 : 90 : ctx->extent_page = *bs_cluster_to_extent_page(blob, cluster_number);
3245 : : }
3246 : :
3247 : 180 : cpl.u.blob_basic.cb_fn = spdk_free_cluster_unmap_complete;
3248 : 180 : cpl.u.blob_basic.cb_arg = ctx;
3249 : : }
3250 : :
3251 : 144718 : batch = bs_batch_open(_ch, &cpl, blob);
3252 [ - + ]: 144718 : if (!batch) {
3253 : 0 : free(ctx);
3254 : 0 : cb_fn(cb_arg, -ENOMEM);
3255 : 0 : return;
3256 : : }
3257 : :
3258 [ + - ]: 144718 : if (is_allocated) {
3259 : 144718 : bs_batch_unmap_dev(batch, lba, lba_count);
3260 : : }
3261 : :
3262 : 144718 : bs_batch_close(batch);
3263 : 144718 : break;
3264 : : }
3265 : 0 : case SPDK_BLOB_READV:
3266 : : case SPDK_BLOB_WRITEV:
3267 : 0 : SPDK_ERRLOG("readv/write not valid\n");
3268 : 0 : cb_fn(cb_arg, -EINVAL);
3269 : 0 : break;
3270 : : }
3271 : 37830 : }
3272 : :
3273 : : static void
3274 : 1849691 : blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel,
3275 : : void *payload, uint64_t offset, uint64_t length,
3276 : : spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
3277 : : {
3278 [ - + ]: 1849691 : assert(blob != NULL);
3279 : :
3280 [ + + + + : 1849691 : if (blob->data_ro && op_type != SPDK_BLOB_READ) {
+ + ]
3281 : 12 : cb_fn(cb_arg, -EPERM);
3282 : 12 : return;
3283 : : }
3284 : :
3285 [ + + ]: 1849679 : if (length == 0) {
3286 : 1504 : cb_fn(cb_arg, 0);
3287 : 1504 : return;
3288 : : }
3289 : :
3290 [ + + ]: 1848175 : if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
3291 : 72 : cb_fn(cb_arg, -EINVAL);
3292 : 72 : return;
3293 : : }
3294 [ + + ]: 1848103 : if (length <= bs_num_io_units_to_cluster_boundary(blob, offset)) {
3295 : 1847535 : blob_request_submit_op_single(_channel, blob, payload, offset, length,
3296 : : cb_fn, cb_arg, op_type);
3297 : : } else {
3298 : 568 : blob_request_submit_op_split(_channel, blob, payload, offset, length,
3299 : : cb_fn, cb_arg, op_type);
3300 : : }
3301 : : }
3302 : :
3303 : : struct rw_iov_ctx {
3304 : : struct spdk_blob *blob;
3305 : : struct spdk_io_channel *channel;
3306 : : spdk_blob_op_complete cb_fn;
3307 : : void *cb_arg;
3308 : : bool read;
3309 : : int iovcnt;
3310 : : struct iovec *orig_iov;
3311 : : uint64_t io_unit_offset;
3312 : : uint64_t io_units_remaining;
3313 : : uint64_t io_units_done;
3314 : : struct spdk_blob_ext_io_opts *ext_io_opts;
3315 : : struct iovec iov[0];
3316 : : };
3317 : :
3318 : : static void
3319 : 5542097 : rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3320 : : {
3321 [ - + ]: 5542097 : assert(cb_arg == NULL);
3322 : 5542097 : bs_sequence_finish(seq, bserrno);
3323 : 5542097 : }
3324 : :
3325 : : static void
3326 : 2232 : rw_iov_split_next(void *cb_arg, int bserrno)
3327 : : {
3328 : 2232 : struct rw_iov_ctx *ctx = cb_arg;
3329 : 2232 : struct spdk_blob *blob = ctx->blob;
3330 : : struct iovec *iov, *orig_iov;
3331 : : int iovcnt;
3332 : : size_t orig_iovoff;
3333 : : uint64_t io_units_count, io_units_to_boundary, io_unit_offset;
3334 : : uint64_t byte_count;
3335 : :
3336 [ + - + + ]: 2232 : if (bserrno != 0 || ctx->io_units_remaining == 0) {
3337 : 612 : ctx->cb_fn(ctx->cb_arg, bserrno);
3338 : 612 : free(ctx);
3339 : 612 : return;
3340 : : }
3341 : :
3342 : 1620 : io_unit_offset = ctx->io_unit_offset;
3343 : 1620 : io_units_to_boundary = bs_num_io_units_to_cluster_boundary(blob, io_unit_offset);
3344 : 1620 : io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary);
3345 : : /*
3346 : : * Get index and offset into the original iov array for our current position in the I/O sequence.
3347 : : * byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will
3348 : : * point to the current position in the I/O sequence.
3349 : : */
3350 : 1620 : byte_count = ctx->io_units_done * blob->bs->io_unit_size;
3351 : 1620 : orig_iov = &ctx->orig_iov[0];
3352 : 1620 : orig_iovoff = 0;
3353 [ + + ]: 3444 : while (byte_count > 0) {
3354 [ + + ]: 1824 : if (byte_count >= orig_iov->iov_len) {
3355 : 1056 : byte_count -= orig_iov->iov_len;
3356 : 1056 : orig_iov++;
3357 : : } else {
3358 : 768 : orig_iovoff = byte_count;
3359 : 768 : byte_count = 0;
3360 : : }
3361 : : }
3362 : :
3363 : : /*
3364 : : * Build an iov array for the next I/O in the sequence. byte_count will keep track of how many
3365 : : * bytes of this next I/O remain to be accounted for in the new iov array.
3366 : : */
3367 : 1620 : byte_count = io_units_count * blob->bs->io_unit_size;
3368 : 1620 : iov = &ctx->iov[0];
3369 : 1620 : iovcnt = 0;
3370 [ + + ]: 4140 : while (byte_count > 0) {
3371 [ - + ]: 2520 : assert(iovcnt < ctx->iovcnt);
3372 : 2520 : iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff);
3373 : 2520 : iov->iov_base = orig_iov->iov_base + orig_iovoff;
3374 : 2520 : byte_count -= iov->iov_len;
3375 : 2520 : orig_iovoff = 0;
3376 : 2520 : orig_iov++;
3377 : 2520 : iov++;
3378 : 2520 : iovcnt++;
3379 : : }
3380 : :
3381 : 1620 : ctx->io_unit_offset += io_units_count;
3382 : 1620 : ctx->io_units_remaining -= io_units_count;
3383 : 1620 : ctx->io_units_done += io_units_count;
3384 : 1620 : iov = &ctx->iov[0];
3385 : :
3386 [ + + + + ]: 1620 : if (ctx->read) {
3387 : 1224 : spdk_blob_io_readv_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
3388 : : io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
3389 : : } else {
3390 : 396 : spdk_blob_io_writev_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
3391 : : io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
3392 : : }
3393 : : }
3394 : :
3395 : : static void
3396 : 5586877 : blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel,
3397 : : struct iovec *iov, int iovcnt,
3398 : : uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg, bool read,
3399 : : struct spdk_blob_ext_io_opts *ext_io_opts)
3400 : : {
3401 : 1926646 : struct spdk_bs_cpl cpl;
3402 : :
3403 [ - + ]: 5586877 : assert(blob != NULL);
3404 : :
3405 [ + + + + : 5586877 : if (!read && blob->data_ro) {
+ + ]
3406 : 45 : cb_fn(cb_arg, -EPERM);
3407 : 8421 : return;
3408 : : }
3409 : :
3410 [ - + ]: 5586832 : if (length == 0) {
3411 : 0 : cb_fn(cb_arg, 0);
3412 : 0 : return;
3413 : : }
3414 : :
3415 [ - + ]: 5586832 : if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
3416 : 0 : cb_fn(cb_arg, -EINVAL);
3417 : 0 : return;
3418 : : }
3419 : :
3420 : : /*
3421 : : * For now, we implement readv/writev using a sequence (instead of a batch) to account for having
3422 : : * to split a request that spans a cluster boundary. For I/O that do not span a cluster boundary,
3423 : : * there will be no noticeable difference compared to using a batch. For I/O that do span a cluster
3424 : : * boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need
3425 : : * to allocate a separate iov array and split the I/O such that none of the resulting
3426 : : * smaller I/O cross a cluster boundary. These smaller I/O will be issued in sequence (not in parallel)
3427 : : * but since this case happens very infrequently, any performance impact will be negligible.
3428 : : *
3429 : : * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs
3430 : : * for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them
3431 : : * in a batch. That would also require creating an intermediate spdk_bs_cpl that would get called
3432 : : * when the batch was completed, to allow for freeing the memory for the iov arrays.
3433 : : */
3434 [ + + ]: 5586832 : if (spdk_likely(length <= bs_num_io_units_to_cluster_boundary(blob, offset))) {
3435 : 1925977 : uint64_t lba_count;
3436 : 1925977 : uint64_t lba;
3437 : : bool is_allocated;
3438 : :
3439 : 5586208 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3440 : 5586208 : cpl.u.blob_basic.cb_fn = cb_fn;
3441 : 5586208 : cpl.u.blob_basic.cb_arg = cb_arg;
3442 : :
3443 [ + + ]: 5586208 : if (blob->frozen_refcnt) {
3444 : : /* This blob I/O is frozen */
3445 : : enum spdk_blob_op_type op_type;
3446 : : spdk_bs_user_op_t *op;
3447 : 1592 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel);
3448 : :
3449 [ - + ]: 1592 : op_type = read ? SPDK_BLOB_READV : SPDK_BLOB_WRITEV;
3450 : 1592 : op = bs_user_op_alloc(_channel, &cpl, op_type, blob, iov, iovcnt, offset, length);
3451 [ - + ]: 1592 : if (!op) {
3452 : 0 : cb_fn(cb_arg, -ENOMEM);
3453 : 8376 : return;
3454 : : }
3455 : :
3456 : 1592 : TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
3457 : :
3458 : 1592 : return;
3459 : : }
3460 : :
3461 : 5584616 : is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
3462 : :
3463 [ + + ]: 5584616 : if (read) {
3464 : : spdk_bs_sequence_t *seq;
3465 : :
3466 : 2814804 : seq = bs_sequence_start_blob(_channel, &cpl, blob);
3467 [ + + ]: 2814804 : if (!seq) {
3468 : 714 : cb_fn(cb_arg, -ENOMEM);
3469 : 714 : return;
3470 : : }
3471 : :
3472 : 2814090 : seq->ext_io_opts = ext_io_opts;
3473 : :
3474 [ + + ]: 2814090 : if (is_allocated) {
3475 : 1486972 : bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
3476 : : } else {
3477 : 1327118 : bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count,
3478 : : rw_iov_done, NULL);
3479 : : }
3480 : : } else {
3481 [ + + ]: 2769812 : if (is_allocated) {
3482 : : spdk_bs_sequence_t *seq;
3483 : :
3484 : 2756954 : seq = bs_sequence_start_blob(_channel, &cpl, blob);
3485 [ + + ]: 2756954 : if (!seq) {
3486 : 28947 : cb_fn(cb_arg, -ENOMEM);
3487 : 28947 : return;
3488 : : }
3489 : :
3490 : 2728007 : seq->ext_io_opts = ext_io_opts;
3491 : :
3492 : 2728007 : bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
3493 : : } else {
3494 : : /* Queue this operation and allocate the cluster */
3495 : : spdk_bs_user_op_t *op;
3496 : :
3497 : 12858 : op = bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset,
3498 : : length);
3499 [ - + ]: 12858 : if (!op) {
3500 : 0 : cb_fn(cb_arg, -ENOMEM);
3501 : 0 : return;
3502 : : }
3503 : :
3504 : 12858 : op->ext_io_opts = ext_io_opts;
3505 : :
3506 : 12858 : bs_allocate_and_copy_cluster(blob, _channel, offset, op);
3507 : : }
3508 : : }
3509 : : } else {
3510 : : struct rw_iov_ctx *ctx;
3511 : :
3512 : 624 : ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec));
3513 [ + + ]: 624 : if (ctx == NULL) {
3514 : 12 : cb_fn(cb_arg, -ENOMEM);
3515 : 12 : return;
3516 : : }
3517 : :
3518 : 612 : ctx->blob = blob;
3519 : 612 : ctx->channel = _channel;
3520 : 612 : ctx->cb_fn = cb_fn;
3521 : 612 : ctx->cb_arg = cb_arg;
3522 : 612 : ctx->read = read;
3523 : 612 : ctx->orig_iov = iov;
3524 : 612 : ctx->iovcnt = iovcnt;
3525 : 612 : ctx->io_unit_offset = offset;
3526 : 612 : ctx->io_units_remaining = length;
3527 : 612 : ctx->io_units_done = 0;
3528 : 612 : ctx->ext_io_opts = ext_io_opts;
3529 : :
3530 : 612 : rw_iov_split_next(ctx, 0);
3531 : : }
3532 : : }
3533 : :
3534 : : static struct spdk_blob *
3535 : 35973 : blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid)
3536 : : {
3537 : 35242 : struct spdk_blob find;
3538 : :
3539 [ + + ]: 35973 : if (spdk_bit_array_get(bs->open_blobids, blobid) == 0) {
3540 : 33399 : return NULL;
3541 : : }
3542 : :
3543 : 2574 : find.id = blobid;
3544 : 2574 : return RB_FIND(spdk_blob_tree, &bs->open_blobs, &find);
3545 : : }
3546 : :
3547 : : static void
3548 : 6189 : blob_get_snapshot_and_clone_entries(struct spdk_blob *blob,
3549 : : struct spdk_blob_list **snapshot_entry, struct spdk_blob_list **clone_entry)
3550 : : {
3551 [ - + ]: 6189 : assert(blob != NULL);
3552 : 6189 : *snapshot_entry = NULL;
3553 : 6189 : *clone_entry = NULL;
3554 : :
3555 [ + + ]: 6189 : if (blob->parent_id == SPDK_BLOBID_INVALID) {
3556 : 5277 : return;
3557 : : }
3558 : :
3559 [ + + ]: 1355 : TAILQ_FOREACH(*snapshot_entry, &blob->bs->snapshots, link) {
3560 [ + + ]: 1182 : if ((*snapshot_entry)->id == blob->parent_id) {
3561 : 739 : break;
3562 : : }
3563 : : }
3564 : :
3565 [ + + ]: 912 : if (*snapshot_entry != NULL) {
3566 [ + - ]: 884 : TAILQ_FOREACH(*clone_entry, &(*snapshot_entry)->clones, link) {
3567 [ + + ]: 884 : if ((*clone_entry)->id == blob->id) {
3568 : 739 : break;
3569 : : }
3570 : : }
3571 : :
3572 [ - + ]: 739 : assert(*clone_entry != NULL);
3573 : : }
3574 : : }
3575 : :
3576 : : static int
3577 : 9109 : bs_channel_create(void *io_device, void *ctx_buf)
3578 : : {
3579 : 9109 : struct spdk_blob_store *bs = io_device;
3580 : 9109 : struct spdk_bs_channel *channel = ctx_buf;
3581 : : struct spdk_bs_dev *dev;
3582 : 9109 : uint32_t max_ops = bs->max_channel_ops;
3583 : : uint32_t i;
3584 : :
3585 : 9109 : dev = bs->dev;
3586 : :
3587 : 9109 : channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set));
3588 [ - + ]: 9109 : if (!channel->req_mem) {
3589 : 0 : return -1;
3590 : : }
3591 : :
3592 : 9109 : TAILQ_INIT(&channel->reqs);
3593 : :
3594 [ + + ]: 4672917 : for (i = 0; i < max_ops; i++) {
3595 : 4663808 : TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
3596 : : }
3597 : :
3598 : 9109 : channel->bs = bs;
3599 : 9109 : channel->dev = dev;
3600 : 9109 : channel->dev_channel = dev->create_channel(dev);
3601 : :
3602 [ - + ]: 9109 : if (!channel->dev_channel) {
3603 : 0 : SPDK_ERRLOG("Failed to create device channel.\n");
3604 : 0 : free(channel->req_mem);
3605 : 0 : return -1;
3606 : : }
3607 : :
3608 : 9109 : channel->new_cluster_page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY,
3609 : : SPDK_MALLOC_DMA);
3610 [ - + ]: 9109 : if (!channel->new_cluster_page) {
3611 : 0 : SPDK_ERRLOG("Failed to allocate new cluster page\n");
3612 : 0 : free(channel->req_mem);
3613 : 0 : channel->dev->destroy_channel(channel->dev, channel->dev_channel);
3614 : 0 : return -1;
3615 : : }
3616 : :
3617 : 9109 : TAILQ_INIT(&channel->need_cluster_alloc);
3618 : 9109 : TAILQ_INIT(&channel->queued_io);
3619 : 9109 : RB_INIT(&channel->esnap_channels);
3620 : :
3621 : 9109 : return 0;
3622 : : }
3623 : :
3624 : : static void
3625 : 9109 : bs_channel_destroy(void *io_device, void *ctx_buf)
3626 : : {
3627 : 9109 : struct spdk_bs_channel *channel = ctx_buf;
3628 : : spdk_bs_user_op_t *op;
3629 : :
3630 [ - + ]: 9109 : while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) {
3631 : 0 : op = TAILQ_FIRST(&channel->need_cluster_alloc);
3632 [ # # ]: 0 : TAILQ_REMOVE(&channel->need_cluster_alloc, op, link);
3633 : 0 : bs_user_op_abort(op, -EIO);
3634 : : }
3635 : :
3636 [ - + ]: 9109 : while (!TAILQ_EMPTY(&channel->queued_io)) {
3637 : 0 : op = TAILQ_FIRST(&channel->queued_io);
3638 [ # # ]: 0 : TAILQ_REMOVE(&channel->queued_io, op, link);
3639 : 0 : bs_user_op_abort(op, -EIO);
3640 : : }
3641 : :
3642 : 9109 : blob_esnap_destroy_bs_channel(channel);
3643 : :
3644 : 9109 : free(channel->req_mem);
3645 : 9109 : spdk_free(channel->new_cluster_page);
3646 : 9109 : channel->dev->destroy_channel(channel->dev, channel->dev_channel);
3647 : 9109 : }
3648 : :
3649 : : static void
3650 : 9001 : bs_dev_destroy(void *io_device)
3651 : : {
3652 : 9001 : struct spdk_blob_store *bs = io_device;
3653 : : struct spdk_blob *blob, *blob_tmp;
3654 : :
3655 : 9001 : bs->dev->destroy(bs->dev);
3656 : :
3657 [ - + - - ]: 9001 : RB_FOREACH_SAFE(blob, spdk_blob_tree, &bs->open_blobs, blob_tmp) {
3658 : 0 : RB_REMOVE(spdk_blob_tree, &bs->open_blobs, blob);
3659 : 0 : spdk_bit_array_clear(bs->open_blobids, blob->id);
3660 : 0 : blob_free(blob);
3661 : : }
3662 : :
3663 : 9001 : spdk_spin_destroy(&bs->used_lock);
3664 : :
3665 : 9001 : spdk_bit_array_free(&bs->open_blobids);
3666 : 9001 : spdk_bit_array_free(&bs->used_blobids);
3667 : 9001 : spdk_bit_array_free(&bs->used_md_pages);
3668 : 9001 : spdk_bit_pool_free(&bs->used_clusters);
3669 : : /*
3670 : : * If this function is called for any reason except a successful unload,
3671 : : * the unload_cpl type will be NONE and this will be a nop.
3672 : : */
3673 : 9001 : bs_call_cpl(&bs->unload_cpl, bs->unload_err);
3674 : :
3675 : 9001 : free(bs);
3676 : 9001 : }
3677 : :
3678 : : static int
3679 : 4466 : bs_blob_list_add(struct spdk_blob *blob)
3680 : : {
3681 : : spdk_blob_id snapshot_id;
3682 : 4466 : struct spdk_blob_list *snapshot_entry = NULL;
3683 : 4466 : struct spdk_blob_list *clone_entry = NULL;
3684 : :
3685 [ - + ]: 4466 : assert(blob != NULL);
3686 : :
3687 : 4466 : snapshot_id = blob->parent_id;
3688 [ + + + + ]: 4466 : if (snapshot_id == SPDK_BLOBID_INVALID ||
3689 : : snapshot_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
3690 : 3146 : return 0;
3691 : : }
3692 : :
3693 : 1320 : snapshot_entry = bs_get_snapshot_entry(blob->bs, snapshot_id);
3694 [ + + ]: 1320 : if (snapshot_entry == NULL) {
3695 : : /* Snapshot not found */
3696 : 903 : snapshot_entry = calloc(1, sizeof(struct spdk_blob_list));
3697 [ - + ]: 903 : if (snapshot_entry == NULL) {
3698 : 0 : return -ENOMEM;
3699 : : }
3700 : 903 : snapshot_entry->id = snapshot_id;
3701 : 903 : TAILQ_INIT(&snapshot_entry->clones);
3702 : 903 : TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link);
3703 : : } else {
3704 [ + + ]: 675 : TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
3705 [ - + ]: 258 : if (clone_entry->id == blob->id) {
3706 : 0 : break;
3707 : : }
3708 : : }
3709 : : }
3710 : :
3711 [ + - ]: 1320 : if (clone_entry == NULL) {
3712 : : /* Clone not found */
3713 : 1320 : clone_entry = calloc(1, sizeof(struct spdk_blob_list));
3714 [ - + ]: 1320 : if (clone_entry == NULL) {
3715 : 0 : return -ENOMEM;
3716 : : }
3717 : 1320 : clone_entry->id = blob->id;
3718 : 1320 : TAILQ_INIT(&clone_entry->clones);
3719 : 1320 : TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link);
3720 : 1320 : snapshot_entry->clone_count++;
3721 : : }
3722 : :
3723 : 1320 : return 0;
3724 : : }
3725 : :
3726 : : static void
3727 : 5946 : bs_blob_list_remove(struct spdk_blob *blob)
3728 : : {
3729 : 5946 : struct spdk_blob_list *snapshot_entry = NULL;
3730 : 5946 : struct spdk_blob_list *clone_entry = NULL;
3731 : :
3732 : 5946 : blob_get_snapshot_and_clone_entries(blob, &snapshot_entry, &clone_entry);
3733 : :
3734 [ + + ]: 5946 : if (snapshot_entry == NULL) {
3735 : 5257 : return;
3736 : : }
3737 : :
3738 : 689 : blob->parent_id = SPDK_BLOBID_INVALID;
3739 [ + + ]: 689 : TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
3740 : 689 : free(clone_entry);
3741 : :
3742 : 689 : snapshot_entry->clone_count--;
3743 : : }
3744 : :
3745 : : static int
3746 : 9001 : bs_blob_list_free(struct spdk_blob_store *bs)
3747 : : {
3748 : : struct spdk_blob_list *snapshot_entry;
3749 : : struct spdk_blob_list *snapshot_entry_tmp;
3750 : : struct spdk_blob_list *clone_entry;
3751 : : struct spdk_blob_list *clone_entry_tmp;
3752 : :
3753 [ + + ]: 9449 : TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) {
3754 [ + + ]: 927 : TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) {
3755 [ + + ]: 479 : TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
3756 : 479 : free(clone_entry);
3757 : : }
3758 [ + + ]: 448 : TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link);
3759 : 448 : free(snapshot_entry);
3760 : : }
3761 : :
3762 : 9001 : return 0;
3763 : : }
3764 : :
3765 : : static void
3766 : 9001 : bs_free(struct spdk_blob_store *bs)
3767 : : {
3768 : 9001 : bs_blob_list_free(bs);
3769 : :
3770 : 9001 : bs_unregister_md_thread(bs);
3771 : 9001 : spdk_io_device_unregister(bs, bs_dev_destroy);
3772 : 9001 : }
3773 : :
3774 : : void
3775 : 16546 : spdk_bs_opts_init(struct spdk_bs_opts *opts, size_t opts_size)
3776 : : {
3777 : :
3778 [ - + ]: 16546 : if (!opts) {
3779 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
3780 : 0 : return;
3781 : : }
3782 : :
3783 [ - + ]: 16546 : if (!opts_size) {
3784 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
3785 : 0 : return;
3786 : : }
3787 : :
3788 [ - + ]: 16546 : memset(opts, 0, opts_size);
3789 : 16546 : opts->opts_size = opts_size;
3790 : :
3791 : : #define FIELD_OK(field) \
3792 : : offsetof(struct spdk_bs_opts, field) + sizeof(opts->field) <= opts_size
3793 : :
3794 : : #define SET_FIELD(field, value) \
3795 : : if (FIELD_OK(field)) { \
3796 : : opts->field = value; \
3797 : : } \
3798 : :
3799 [ + - ]: 16546 : SET_FIELD(cluster_sz, SPDK_BLOB_OPTS_CLUSTER_SZ);
3800 [ + - ]: 16546 : SET_FIELD(num_md_pages, SPDK_BLOB_OPTS_NUM_MD_PAGES);
3801 [ + - ]: 16546 : SET_FIELD(max_md_ops, SPDK_BLOB_OPTS_NUM_MD_PAGES);
3802 [ + - ]: 16546 : SET_FIELD(max_channel_ops, SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS);
3803 [ + - ]: 16546 : SET_FIELD(clear_method, BS_CLEAR_WITH_UNMAP);
3804 : :
3805 [ + - ]: 16546 : if (FIELD_OK(bstype)) {
3806 [ - + ]: 16546 : memset(&opts->bstype, 0, sizeof(opts->bstype));
3807 : : }
3808 : :
3809 [ + - ]: 16546 : SET_FIELD(iter_cb_fn, NULL);
3810 [ + - ]: 16546 : SET_FIELD(iter_cb_arg, NULL);
3811 [ + - ]: 16546 : SET_FIELD(force_recover, false);
3812 [ + - ]: 16546 : SET_FIELD(esnap_bs_dev_create, NULL);
3813 [ + - ]: 16546 : SET_FIELD(esnap_ctx, NULL);
3814 : :
3815 : : #undef FIELD_OK
3816 : : #undef SET_FIELD
3817 : : }
3818 : :
3819 : : static int
3820 : 1602 : bs_opts_verify(struct spdk_bs_opts *opts)
3821 : : {
3822 [ + + + - : 1602 : if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 ||
+ - ]
3823 [ - + ]: 1590 : opts->max_channel_ops == 0) {
3824 : 12 : SPDK_ERRLOG("Blobstore options cannot be set to 0\n");
3825 : 12 : return -1;
3826 : : }
3827 : :
3828 : 1590 : return 0;
3829 : : }
3830 : :
3831 : : /* START spdk_bs_load */
3832 : :
3833 : : /* spdk_bs_load_ctx is used for init, load, unload and dump code paths. */
3834 : :
3835 : : struct spdk_bs_load_ctx {
3836 : : struct spdk_blob_store *bs;
3837 : : struct spdk_bs_super_block *super;
3838 : :
3839 : : struct spdk_bs_md_mask *mask;
3840 : : bool in_page_chain;
3841 : : uint32_t page_index;
3842 : : uint32_t cur_page;
3843 : : struct spdk_blob_md_page *page;
3844 : :
3845 : : uint64_t num_extent_pages;
3846 : : uint32_t *extent_page_num;
3847 : : struct spdk_blob_md_page *extent_pages;
3848 : : struct spdk_bit_array *used_clusters;
3849 : :
3850 : : spdk_bs_sequence_t *seq;
3851 : : spdk_blob_op_with_handle_complete iter_cb_fn;
3852 : : void *iter_cb_arg;
3853 : : struct spdk_blob *blob;
3854 : : spdk_blob_id blobid;
3855 : :
3856 : : bool force_recover;
3857 : :
3858 : : /* These fields are used in the spdk_bs_dump path. */
3859 : : bool dumping;
3860 : : FILE *fp;
3861 : : spdk_bs_dump_print_xattr print_xattr_fn;
3862 : : char xattr_name[4096];
3863 : : };
3864 : :
3865 : : static int
3866 : 9059 : bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs,
3867 : : struct spdk_bs_load_ctx **_ctx)
3868 : : {
3869 : : struct spdk_blob_store *bs;
3870 : : struct spdk_bs_load_ctx *ctx;
3871 : : uint64_t dev_size;
3872 : : int rc;
3873 : :
3874 : 9059 : dev_size = dev->blocklen * dev->blockcnt;
3875 [ + + ]: 9059 : if (dev_size < opts->cluster_sz) {
3876 : : /* Device size cannot be smaller than cluster size of blobstore */
3877 [ - + - + ]: 46 : SPDK_INFOLOG(blob, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n",
3878 : : dev_size, opts->cluster_sz);
3879 : 46 : return -ENOSPC;
3880 : : }
3881 [ + + ]: 9013 : if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) {
3882 : : /* Cluster size cannot be smaller than page size */
3883 : 12 : SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n",
3884 : : opts->cluster_sz, SPDK_BS_PAGE_SIZE);
3885 : 12 : return -EINVAL;
3886 : : }
3887 : 9001 : bs = calloc(1, sizeof(struct spdk_blob_store));
3888 [ - + ]: 9001 : if (!bs) {
3889 : 0 : return -ENOMEM;
3890 : : }
3891 : :
3892 : 9001 : ctx = calloc(1, sizeof(struct spdk_bs_load_ctx));
3893 [ - + ]: 9001 : if (!ctx) {
3894 : 0 : free(bs);
3895 : 0 : return -ENOMEM;
3896 : : }
3897 : :
3898 : 9001 : ctx->bs = bs;
3899 : 9001 : ctx->iter_cb_fn = opts->iter_cb_fn;
3900 : 9001 : ctx->iter_cb_arg = opts->iter_cb_arg;
3901 : 9001 : ctx->force_recover = opts->force_recover;
3902 : :
3903 : 9001 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
3904 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
3905 [ - + ]: 9001 : if (!ctx->super) {
3906 : 0 : free(ctx);
3907 : 0 : free(bs);
3908 : 0 : return -ENOMEM;
3909 : : }
3910 : :
3911 : 9001 : RB_INIT(&bs->open_blobs);
3912 : 9001 : TAILQ_INIT(&bs->snapshots);
3913 : 9001 : bs->dev = dev;
3914 : 9001 : bs->md_thread = spdk_get_thread();
3915 [ - + ]: 9001 : assert(bs->md_thread != NULL);
3916 : :
3917 : : /*
3918 : : * Do not use bs_lba_to_cluster() here since blockcnt may not be an
3919 : : * even multiple of the cluster size.
3920 : : */
3921 : 9001 : bs->cluster_sz = opts->cluster_sz;
3922 [ - + - + ]: 9001 : bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
3923 : 9001 : ctx->used_clusters = spdk_bit_array_create(bs->total_clusters);
3924 [ - + ]: 9001 : if (!ctx->used_clusters) {
3925 : 0 : spdk_free(ctx->super);
3926 : 0 : free(ctx);
3927 : 0 : free(bs);
3928 : 0 : return -ENOMEM;
3929 : : }
3930 : :
3931 : 9001 : bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
3932 [ + - ]: 9001 : if (spdk_u32_is_pow2(bs->pages_per_cluster)) {
3933 : 9001 : bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster);
3934 : : }
3935 : 9001 : bs->num_free_clusters = bs->total_clusters;
3936 : 9001 : bs->io_unit_size = dev->blocklen;
3937 : :
3938 : 9001 : bs->max_channel_ops = opts->max_channel_ops;
3939 : 9001 : bs->super_blob = SPDK_BLOBID_INVALID;
3940 : 9001 : memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
3941 : 9001 : bs->esnap_bs_dev_create = opts->esnap_bs_dev_create;
3942 : 9001 : bs->esnap_ctx = opts->esnap_ctx;
3943 : :
3944 : : /* The metadata is assumed to be at least 1 page */
3945 : 9001 : bs->used_md_pages = spdk_bit_array_create(1);
3946 : 9001 : bs->used_blobids = spdk_bit_array_create(0);
3947 : 9001 : bs->open_blobids = spdk_bit_array_create(0);
3948 : :
3949 : 9001 : spdk_spin_init(&bs->used_lock);
3950 : :
3951 : 9001 : spdk_io_device_register(bs, bs_channel_create, bs_channel_destroy,
3952 : : sizeof(struct spdk_bs_channel), "blobstore");
3953 : 9001 : rc = bs_register_md_thread(bs);
3954 [ - + ]: 9001 : if (rc == -1) {
3955 : 0 : spdk_io_device_unregister(bs, NULL);
3956 : 0 : spdk_spin_destroy(&bs->used_lock);
3957 : 0 : spdk_bit_array_free(&bs->open_blobids);
3958 : 0 : spdk_bit_array_free(&bs->used_blobids);
3959 : 0 : spdk_bit_array_free(&bs->used_md_pages);
3960 : 0 : spdk_bit_array_free(&ctx->used_clusters);
3961 : 0 : spdk_free(ctx->super);
3962 : 0 : free(ctx);
3963 : 0 : free(bs);
3964 : : /* FIXME: this is a lie but don't know how to get a proper error code here */
3965 : 0 : return -ENOMEM;
3966 : : }
3967 : :
3968 : 9001 : *_ctx = ctx;
3969 : 9001 : *_bs = bs;
3970 : 9001 : return 0;
3971 : : }
3972 : :
3973 : : static void
3974 : 6500 : bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno)
3975 : : {
3976 [ - + ]: 6500 : assert(bserrno != 0);
3977 : :
3978 : 6500 : spdk_free(ctx->super);
3979 : 6500 : bs_sequence_finish(ctx->seq, bserrno);
3980 : 6500 : bs_free(ctx->bs);
3981 : 6500 : spdk_bit_array_free(&ctx->used_clusters);
3982 : 6500 : free(ctx);
3983 : 6500 : }
3984 : :
3985 : : static void
3986 : 2798 : bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
3987 : : struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
3988 : : {
3989 : : /* Update the values in the super block */
3990 : 2798 : super->super_blob = bs->super_blob;
3991 : 2798 : memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
3992 : 2798 : super->crc = blob_md_page_calc_crc(super);
3993 : 2798 : bs_sequence_write_dev(seq, super, bs_page_to_lba(bs, 0),
3994 : 2798 : bs_byte_to_lba(bs, sizeof(*super)),
3995 : : cb_fn, cb_arg);
3996 : 2798 : }
3997 : :
3998 : : static void
3999 : 2421 : bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
4000 : : {
4001 : 2421 : struct spdk_bs_load_ctx *ctx = arg;
4002 : : uint64_t mask_size, lba, lba_count;
4003 : :
4004 : : /* Write out the used clusters mask */
4005 : 2421 : mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
4006 : 2421 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
4007 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4008 [ - + ]: 2421 : if (!ctx->mask) {
4009 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4010 : 0 : return;
4011 : : }
4012 : :
4013 : 2421 : ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
4014 : 2421 : ctx->mask->length = ctx->bs->total_clusters;
4015 : : /* We could get here through the normal unload path, or through dirty
4016 : : * shutdown recovery. For the normal unload path, we use the mask from
4017 : : * the bit pool. For dirty shutdown recovery, we don't have a bit pool yet -
4018 : : * only the bit array from the load ctx.
4019 : : */
4020 [ + + ]: 2421 : if (ctx->bs->used_clusters) {
4021 [ - + ]: 2098 : assert(ctx->mask->length == spdk_bit_pool_capacity(ctx->bs->used_clusters));
4022 : 2098 : spdk_bit_pool_store_mask(ctx->bs->used_clusters, ctx->mask->mask);
4023 : : } else {
4024 [ - + ]: 323 : assert(ctx->mask->length == spdk_bit_array_capacity(ctx->used_clusters));
4025 : 323 : spdk_bit_array_store_mask(ctx->used_clusters, ctx->mask->mask);
4026 : : }
4027 : 2421 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
4028 : 2421 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
4029 : 2421 : bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
4030 : : }
4031 : :
4032 : : static void
4033 : 2421 : bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
4034 : : {
4035 : 2421 : struct spdk_bs_load_ctx *ctx = arg;
4036 : : uint64_t mask_size, lba, lba_count;
4037 : :
4038 : 2421 : mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
4039 : 2421 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
4040 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4041 [ - + ]: 2421 : if (!ctx->mask) {
4042 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4043 : 0 : return;
4044 : : }
4045 : :
4046 : 2421 : ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
4047 : 2421 : ctx->mask->length = ctx->super->md_len;
4048 [ - + ]: 2421 : assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
4049 : :
4050 : 2421 : spdk_bit_array_store_mask(ctx->bs->used_md_pages, ctx->mask->mask);
4051 : 2421 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
4052 : 2421 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
4053 : 2421 : bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
4054 : : }
4055 : :
4056 : : static void
4057 : 2421 : bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
4058 : : {
4059 : 2421 : struct spdk_bs_load_ctx *ctx = arg;
4060 : : uint64_t mask_size, lba, lba_count;
4061 : :
4062 [ + + ]: 2421 : if (ctx->super->used_blobid_mask_len == 0) {
4063 : : /*
4064 : : * This is a pre-v3 on-disk format where the blobid mask does not get
4065 : : * written to disk.
4066 : : */
4067 : 72 : cb_fn(seq, arg, 0);
4068 : 72 : return;
4069 : : }
4070 : :
4071 : 2349 : mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
4072 : 2349 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
4073 : : SPDK_MALLOC_DMA);
4074 [ - + ]: 2349 : if (!ctx->mask) {
4075 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4076 : 0 : return;
4077 : : }
4078 : :
4079 : 2349 : ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS;
4080 : 2349 : ctx->mask->length = ctx->super->md_len;
4081 [ - + ]: 2349 : assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids));
4082 : :
4083 : 2349 : spdk_bit_array_store_mask(ctx->bs->used_blobids, ctx->mask->mask);
4084 : 2349 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
4085 : 2349 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
4086 : 2349 : bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
4087 : : }
4088 : :
4089 : : static void
4090 : 2251 : blob_set_thin_provision(struct spdk_blob *blob)
4091 : : {
4092 : 2251 : blob_verify_md_op(blob);
4093 : 2251 : blob->invalid_flags |= SPDK_BLOB_THIN_PROV;
4094 : 2251 : blob->state = SPDK_BLOB_STATE_DIRTY;
4095 : 2251 : }
4096 : :
4097 : : static void
4098 : 7477 : blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method)
4099 : : {
4100 : 7477 : blob_verify_md_op(blob);
4101 : 7477 : blob->clear_method = clear_method;
4102 : 7477 : blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT);
4103 : 7477 : blob->state = SPDK_BLOB_STATE_DIRTY;
4104 : 7477 : }
4105 : :
4106 : : static void bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno);
4107 : :
4108 : : static void
4109 : 72 : bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno)
4110 : : {
4111 : 72 : struct spdk_bs_load_ctx *ctx = cb_arg;
4112 : : spdk_blob_id id;
4113 : : int64_t page_num;
4114 : :
4115 : : /* Iterate to next blob (we can't use spdk_bs_iter_next function as our
4116 : : * last blob has been removed */
4117 : 72 : page_num = bs_blobid_to_page(ctx->blobid);
4118 : 72 : page_num++;
4119 : 72 : page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num);
4120 [ + - ]: 72 : if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) {
4121 : 72 : bs_load_iter(ctx, NULL, -ENOENT);
4122 : 72 : return;
4123 : : }
4124 : :
4125 : 0 : id = bs_page_to_blobid(page_num);
4126 : :
4127 : 0 : spdk_bs_open_blob(ctx->bs, id, bs_load_iter, ctx);
4128 : : }
4129 : :
4130 : : static void
4131 : 72 : bs_delete_corrupted_close_cb(void *cb_arg, int bserrno)
4132 : : {
4133 : 72 : struct spdk_bs_load_ctx *ctx = cb_arg;
4134 : :
4135 [ - + ]: 72 : if (bserrno != 0) {
4136 : 0 : SPDK_ERRLOG("Failed to close corrupted blob\n");
4137 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4138 : 0 : return;
4139 : : }
4140 : :
4141 : 72 : spdk_bs_delete_blob(ctx->bs, ctx->blobid, bs_delete_corrupted_blob_cpl, ctx);
4142 : : }
4143 : :
4144 : : static void
4145 : 72 : bs_delete_corrupted_blob(void *cb_arg, int bserrno)
4146 : : {
4147 : 72 : struct spdk_bs_load_ctx *ctx = cb_arg;
4148 : : uint64_t i;
4149 : :
4150 [ - + ]: 72 : if (bserrno != 0) {
4151 : 0 : SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
4152 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4153 : 0 : return;
4154 : : }
4155 : :
4156 : : /* Snapshot and clone have the same copy of cluster map and extent pages
4157 : : * at this point. Let's clear both for snapshot now,
4158 : : * so that it won't be cleared for clone later when we remove snapshot.
4159 : : * Also set thin provision to pass data corruption check */
4160 [ + + ]: 792 : for (i = 0; i < ctx->blob->active.num_clusters; i++) {
4161 : 720 : ctx->blob->active.clusters[i] = 0;
4162 : : }
4163 [ + + ]: 108 : for (i = 0; i < ctx->blob->active.num_extent_pages; i++) {
4164 : 36 : ctx->blob->active.extent_pages[i] = 0;
4165 : : }
4166 : :
4167 : 72 : ctx->blob->active.num_allocated_clusters = 0;
4168 : :
4169 : 72 : ctx->blob->md_ro = false;
4170 : :
4171 : 72 : blob_set_thin_provision(ctx->blob);
4172 : :
4173 : 72 : ctx->blobid = ctx->blob->id;
4174 : :
4175 : 72 : spdk_blob_close(ctx->blob, bs_delete_corrupted_close_cb, ctx);
4176 : : }
4177 : :
4178 : : static void
4179 : 36 : bs_update_corrupted_blob(void *cb_arg, int bserrno)
4180 : : {
4181 : 36 : struct spdk_bs_load_ctx *ctx = cb_arg;
4182 : :
4183 [ - + ]: 36 : if (bserrno != 0) {
4184 : 0 : SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
4185 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4186 : 0 : return;
4187 : : }
4188 : :
4189 : 36 : ctx->blob->md_ro = false;
4190 : 36 : blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true);
4191 : 36 : blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true);
4192 : 36 : spdk_blob_set_read_only(ctx->blob);
4193 : :
4194 [ - + ]: 36 : if (ctx->iter_cb_fn) {
4195 : 0 : ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0);
4196 : : }
4197 : 36 : bs_blob_list_add(ctx->blob);
4198 : :
4199 : 36 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4200 : : }
4201 : :
4202 : : static void
4203 : 108 : bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno)
4204 : : {
4205 : 108 : struct spdk_bs_load_ctx *ctx = cb_arg;
4206 : :
4207 [ - + ]: 108 : if (bserrno != 0) {
4208 : 0 : SPDK_ERRLOG("Failed to open clone of a corrupted blob\n");
4209 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4210 : 0 : return;
4211 : : }
4212 : :
4213 [ + + ]: 108 : if (blob->parent_id == ctx->blob->id) {
4214 : : /* Power failure occurred before updating clone (snapshot delete case)
4215 : : * or after updating clone (creating snapshot case) - keep snapshot */
4216 : 36 : spdk_blob_close(blob, bs_update_corrupted_blob, ctx);
4217 : : } else {
4218 : : /* Power failure occurred after updating clone (snapshot delete case)
4219 : : * or before updating clone (creating snapshot case) - remove snapshot */
4220 : 72 : spdk_blob_close(blob, bs_delete_corrupted_blob, ctx);
4221 : : }
4222 : : }
4223 : :
4224 : : static void
4225 : 3909 : bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno)
4226 : : {
4227 : 3909 : struct spdk_bs_load_ctx *ctx = arg;
4228 : 3869 : const void *value;
4229 : 3869 : size_t len;
4230 : 3909 : int rc = 0;
4231 : :
4232 [ + + ]: 3909 : if (bserrno == 0) {
4233 : : /* Examine blob if it is corrupted after power failure. Fix
4234 : : * the ones that can be fixed and remove any other corrupted
4235 : : * ones. If it is not corrupted just process it */
4236 : 2986 : rc = blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true);
4237 [ + + ]: 2986 : if (rc != 0) {
4238 : 2926 : rc = blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true);
4239 [ + + ]: 2926 : if (rc != 0) {
4240 : : /* Not corrupted - process it and continue with iterating through blobs */
4241 [ + + ]: 2878 : if (ctx->iter_cb_fn) {
4242 : 1267 : ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0);
4243 : : }
4244 : 2878 : bs_blob_list_add(blob);
4245 : 2878 : spdk_bs_iter_next(ctx->bs, blob, bs_load_iter, ctx);
4246 : 2878 : return;
4247 : : }
4248 : :
4249 : : }
4250 : :
4251 [ - + ]: 108 : assert(len == sizeof(spdk_blob_id));
4252 : :
4253 : 108 : ctx->blob = blob;
4254 : :
4255 : : /* Open clone to check if we are able to fix this blob or should we remove it */
4256 : 108 : spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, bs_examine_clone, ctx);
4257 : 108 : return;
4258 [ + - ]: 923 : } else if (bserrno == -ENOENT) {
4259 : 923 : bserrno = 0;
4260 : : } else {
4261 : : /*
4262 : : * This case needs to be looked at further. Same problem
4263 : : * exists with applications that rely on explicit blob
4264 : : * iteration. We should just skip the blob that failed
4265 : : * to load and continue on to the next one.
4266 : : */
4267 : 0 : SPDK_ERRLOG("Error in iterating blobs\n");
4268 : : }
4269 : :
4270 : 923 : ctx->iter_cb_fn = NULL;
4271 : :
4272 : 923 : spdk_free(ctx->super);
4273 : 923 : spdk_free(ctx->mask);
4274 : 923 : bs_sequence_finish(ctx->seq, bserrno);
4275 : 923 : free(ctx);
4276 : : }
4277 : :
4278 : : static void bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg);
4279 : :
4280 : : static void
4281 : 923 : bs_load_complete(struct spdk_bs_load_ctx *ctx)
4282 : : {
4283 : 923 : ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
4284 [ - + - + ]: 923 : if (ctx->dumping) {
4285 : 0 : bs_dump_read_md_page(ctx->seq, ctx);
4286 : 0 : return;
4287 : : }
4288 : 923 : spdk_bs_iter_first(ctx->bs, bs_load_iter, ctx);
4289 : : }
4290 : :
4291 : : static void
4292 : 600 : bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4293 : : {
4294 : 600 : struct spdk_bs_load_ctx *ctx = cb_arg;
4295 : : int rc;
4296 : :
4297 : : /* The type must be correct */
4298 [ - + ]: 600 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS);
4299 : :
4300 : : /* The length of the mask (in bits) must not be greater than
4301 : : * the length of the buffer (converted to bits) */
4302 [ - + ]: 600 : assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8));
4303 : :
4304 : : /* The length of the mask must be exactly equal to the size
4305 : : * (in pages) of the metadata region */
4306 [ - + ]: 600 : assert(ctx->mask->length == ctx->super->md_len);
4307 : :
4308 : 600 : rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length);
4309 [ - + ]: 600 : if (rc < 0) {
4310 : 0 : spdk_free(ctx->mask);
4311 : 0 : bs_load_ctx_fail(ctx, rc);
4312 : 0 : return;
4313 : : }
4314 : :
4315 : 600 : spdk_bit_array_load_mask(ctx->bs->used_blobids, ctx->mask->mask);
4316 : 600 : bs_load_complete(ctx);
4317 : : }
4318 : :
4319 : : static void
4320 : 600 : bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4321 : : {
4322 : 600 : struct spdk_bs_load_ctx *ctx = cb_arg;
4323 : : uint64_t lba, lba_count, mask_size;
4324 : : int rc;
4325 : :
4326 [ - + ]: 600 : if (bserrno != 0) {
4327 : 0 : bs_load_ctx_fail(ctx, bserrno);
4328 : 0 : return;
4329 : : }
4330 : :
4331 : : /* The type must be correct */
4332 [ - + ]: 600 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
4333 : : /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
4334 [ - + ]: 600 : assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
4335 : : struct spdk_blob_md_page) * 8));
4336 : : /*
4337 : : * The length of the mask must be equal to or larger than the total number of clusters. It may be
4338 : : * larger than the total number of clusters due to a failure spdk_bs_grow.
4339 : : */
4340 [ - + ]: 600 : assert(ctx->mask->length >= ctx->bs->total_clusters);
4341 [ + + ]: 600 : if (ctx->mask->length > ctx->bs->total_clusters) {
4342 : 12 : SPDK_WARNLOG("Shrink the used_custers mask length to total_clusters");
4343 : 12 : ctx->mask->length = ctx->bs->total_clusters;
4344 : : }
4345 : :
4346 : 600 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->mask->length);
4347 [ - + ]: 600 : if (rc < 0) {
4348 : 0 : spdk_free(ctx->mask);
4349 : 0 : bs_load_ctx_fail(ctx, rc);
4350 : 0 : return;
4351 : : }
4352 : :
4353 : 600 : spdk_bit_array_load_mask(ctx->used_clusters, ctx->mask->mask);
4354 : 600 : ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->used_clusters);
4355 [ - + ]: 600 : assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
4356 : :
4357 : 600 : spdk_free(ctx->mask);
4358 : :
4359 : : /* Read the used blobids mask */
4360 : 600 : mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
4361 : 600 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
4362 : : SPDK_MALLOC_DMA);
4363 [ - + ]: 600 : if (!ctx->mask) {
4364 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4365 : 0 : return;
4366 : : }
4367 : 600 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
4368 : 600 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
4369 : 600 : bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
4370 : : bs_load_used_blobids_cpl, ctx);
4371 : : }
4372 : :
4373 : : static void
4374 : 600 : bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4375 : : {
4376 : 600 : struct spdk_bs_load_ctx *ctx = cb_arg;
4377 : : uint64_t lba, lba_count, mask_size;
4378 : : int rc;
4379 : :
4380 [ - + ]: 600 : if (bserrno != 0) {
4381 : 0 : bs_load_ctx_fail(ctx, bserrno);
4382 : 0 : return;
4383 : : }
4384 : :
4385 : : /* The type must be correct */
4386 [ - + ]: 600 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
4387 : : /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
4388 [ - + ]: 600 : assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
4389 : : 8));
4390 : : /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
4391 [ - + ]: 600 : if (ctx->mask->length != ctx->super->md_len) {
4392 : 0 : SPDK_ERRLOG("mismatched md_len in used_pages mask: "
4393 : : "mask->length=%" PRIu32 " super->md_len=%" PRIu32 "\n",
4394 : : ctx->mask->length, ctx->super->md_len);
4395 : 0 : assert(false);
4396 : : }
4397 : :
4398 : 600 : rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length);
4399 [ - + ]: 600 : if (rc < 0) {
4400 : 0 : spdk_free(ctx->mask);
4401 : 0 : bs_load_ctx_fail(ctx, rc);
4402 : 0 : return;
4403 : : }
4404 : :
4405 : 600 : spdk_bit_array_load_mask(ctx->bs->used_md_pages, ctx->mask->mask);
4406 : 600 : spdk_free(ctx->mask);
4407 : :
4408 : : /* Read the used clusters mask */
4409 : 600 : mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
4410 : 600 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
4411 : : SPDK_MALLOC_DMA);
4412 [ - + ]: 600 : if (!ctx->mask) {
4413 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4414 : 0 : return;
4415 : : }
4416 : 600 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
4417 : 600 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
4418 : 600 : bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
4419 : : bs_load_used_clusters_cpl, ctx);
4420 : : }
4421 : :
4422 : : static void
4423 : 600 : bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx)
4424 : : {
4425 : : uint64_t lba, lba_count, mask_size;
4426 : :
4427 : : /* Read the used pages mask */
4428 : 600 : mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
4429 : 600 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
4430 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4431 [ - + ]: 600 : if (!ctx->mask) {
4432 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4433 : 0 : return;
4434 : : }
4435 : :
4436 : 600 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
4437 : 600 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
4438 : 600 : bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
4439 : : bs_load_used_pages_cpl, ctx);
4440 : : }
4441 : :
4442 : : static int
4443 : 754 : bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page)
4444 : : {
4445 : 754 : struct spdk_blob_store *bs = ctx->bs;
4446 : : struct spdk_blob_md_descriptor *desc;
4447 : 754 : size_t cur_desc = 0;
4448 : :
4449 : 754 : desc = (struct spdk_blob_md_descriptor *)page->descriptors;
4450 [ + - ]: 2216 : while (cur_desc < sizeof(page->descriptors)) {
4451 [ + + ]: 2216 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
4452 [ + - ]: 694 : if (desc->length == 0) {
4453 : : /* If padding and length are 0, this terminates the page */
4454 : 694 : break;
4455 : : }
4456 [ + + ]: 1522 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
4457 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
4458 : : unsigned int i, j;
4459 : 204 : unsigned int cluster_count = 0;
4460 : : uint32_t cluster_idx;
4461 : :
4462 : 204 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
4463 : :
4464 [ + + ]: 408 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
4465 [ + + ]: 2484 : for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
4466 : 2280 : cluster_idx = desc_extent_rle->extents[i].cluster_idx;
4467 : : /*
4468 : : * cluster_idx = 0 means an unallocated cluster - don't mark that
4469 : : * in the used cluster map.
4470 : : */
4471 [ + + ]: 2280 : if (cluster_idx != 0) {
4472 : 1620 : SPDK_NOTICELOG("Recover: cluster %" PRIu32 "\n", cluster_idx + j);
4473 : 1620 : spdk_bit_array_set(ctx->used_clusters, cluster_idx + j);
4474 [ - + ]: 1620 : if (bs->num_free_clusters == 0) {
4475 : 0 : return -ENOSPC;
4476 : : }
4477 : 1620 : bs->num_free_clusters--;
4478 : : }
4479 : 2280 : cluster_count++;
4480 : : }
4481 : : }
4482 [ - + ]: 204 : if (cluster_count == 0) {
4483 : 0 : return -EINVAL;
4484 : : }
4485 [ + + ]: 1318 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
4486 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
4487 : : uint32_t i;
4488 : 162 : uint32_t cluster_count = 0;
4489 : : uint32_t cluster_idx;
4490 : : size_t cluster_idx_length;
4491 : :
4492 : 162 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
4493 : 162 : cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
4494 : :
4495 [ + - ]: 162 : if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
4496 [ - + ]: 162 : (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
4497 : 0 : return -EINVAL;
4498 : : }
4499 : :
4500 [ + + ]: 2882 : for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
4501 : 2720 : cluster_idx = desc_extent->cluster_idx[i];
4502 : : /*
4503 : : * cluster_idx = 0 means an unallocated cluster - don't mark that
4504 : : * in the used cluster map.
4505 : : */
4506 [ + + ]: 2720 : if (cluster_idx != 0) {
4507 [ + + ]: 1954 : if (cluster_idx < desc_extent->start_cluster_idx &&
4508 [ - + ]: 1 : cluster_idx >= desc_extent->start_cluster_idx + cluster_count) {
4509 : 0 : return -EINVAL;
4510 : : }
4511 : 1954 : spdk_bit_array_set(ctx->used_clusters, cluster_idx);
4512 [ - + ]: 1954 : if (bs->num_free_clusters == 0) {
4513 : 0 : return -ENOSPC;
4514 : : }
4515 : 1954 : bs->num_free_clusters--;
4516 : : }
4517 : 2720 : cluster_count++;
4518 : : }
4519 : :
4520 [ - + ]: 162 : if (cluster_count == 0) {
4521 : 0 : return -EINVAL;
4522 : : }
4523 [ + + ]: 1156 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
4524 : : /* Skip this item */
4525 [ + + ]: 908 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
4526 : : /* Skip this item */
4527 [ + + ]: 728 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
4528 : : /* Skip this item */
4529 [ + - ]: 256 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
4530 : : struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
4531 : 256 : uint32_t num_extent_pages = ctx->num_extent_pages;
4532 : : uint32_t i;
4533 : : size_t extent_pages_length;
4534 : : void *tmp;
4535 : :
4536 : 256 : desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
4537 : 256 : extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
4538 : :
4539 [ + - ]: 256 : if (desc_extent_table->length == 0 ||
4540 [ - + ]: 256 : (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
4541 : 0 : return -EINVAL;
4542 : : }
4543 : :
4544 [ + + ]: 497 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
4545 [ + + ]: 241 : if (desc_extent_table->extent_page[i].page_idx != 0) {
4546 [ - + ]: 162 : if (desc_extent_table->extent_page[i].num_pages != 1) {
4547 : 0 : return -EINVAL;
4548 : : }
4549 : 162 : num_extent_pages += 1;
4550 : : }
4551 : : }
4552 : :
4553 [ + + ]: 256 : if (num_extent_pages > 0) {
4554 : 161 : tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t));
4555 [ - + ]: 161 : if (tmp == NULL) {
4556 : 0 : return -ENOMEM;
4557 : : }
4558 : 161 : ctx->extent_page_num = tmp;
4559 : :
4560 : : /* Extent table entries contain md page numbers for extent pages.
4561 : : * Zeroes represent unallocated extent pages, those are run-length-encoded.
4562 : : */
4563 [ + + ]: 324 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
4564 [ + + ]: 163 : if (desc_extent_table->extent_page[i].page_idx != 0) {
4565 : 162 : ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx;
4566 : 162 : ctx->num_extent_pages += 1;
4567 : : }
4568 : : }
4569 : : }
4570 : : } else {
4571 : : /* Error */
4572 : 0 : return -EINVAL;
4573 : : }
4574 : : /* Advance to the next descriptor */
4575 : 1522 : cur_desc += sizeof(*desc) + desc->length;
4576 [ + + ]: 1522 : if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
4577 : 60 : break;
4578 : : }
4579 : 1462 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
4580 : : }
4581 : 754 : return 0;
4582 : : }
4583 : :
4584 : : static bool
4585 : 9780 : bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page)
4586 : : {
4587 : : uint32_t crc;
4588 : 9780 : struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors;
4589 : : size_t desc_len;
4590 : :
4591 : 9780 : crc = blob_md_page_calc_crc(page);
4592 [ - + ]: 9780 : if (crc != page->crc) {
4593 : 0 : return false;
4594 : : }
4595 : :
4596 : : /* Extent page should always be of sequence num 0. */
4597 [ + + ]: 9780 : if (page->sequence_num != 0) {
4598 : 132 : return false;
4599 : : }
4600 : :
4601 : : /* Descriptor type must be EXTENT_PAGE. */
4602 [ + + ]: 9648 : if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
4603 : 472 : return false;
4604 : : }
4605 : :
4606 : : /* Descriptor length cannot exceed the page. */
4607 : 9176 : desc_len = sizeof(*desc) + desc->length;
4608 [ - + ]: 9176 : if (desc_len > sizeof(page->descriptors)) {
4609 : 0 : return false;
4610 : : }
4611 : :
4612 : : /* It has to be the only descriptor in the page. */
4613 [ + - ]: 9176 : if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) {
4614 : 9176 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len);
4615 [ - + ]: 9176 : if (desc->length != 0) {
4616 : 0 : return false;
4617 : : }
4618 : : }
4619 : :
4620 : 9176 : return true;
4621 : : }
4622 : :
4623 : : static bool
4624 : 22136 : bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx)
4625 : : {
4626 : : uint32_t crc;
4627 : 22136 : struct spdk_blob_md_page *page = ctx->page;
4628 : :
4629 : 22136 : crc = blob_md_page_calc_crc(page);
4630 [ + + ]: 22136 : if (crc != page->crc) {
4631 : 21478 : return false;
4632 : : }
4633 : :
4634 : : /* First page of a sequence should match the blobid. */
4635 [ + + ]: 658 : if (page->sequence_num == 0 &&
4636 [ + + ]: 526 : bs_page_to_blobid(ctx->cur_page) != page->id) {
4637 : 54 : return false;
4638 : : }
4639 [ - + ]: 604 : assert(bs_load_cur_extent_page_valid(page) == false);
4640 : :
4641 : 604 : return true;
4642 : : }
4643 : :
4644 : : static void bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx);
4645 : :
4646 : : static void
4647 : 323 : bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4648 : : {
4649 : 323 : struct spdk_bs_load_ctx *ctx = cb_arg;
4650 : :
4651 [ - + ]: 323 : if (bserrno != 0) {
4652 : 0 : bs_load_ctx_fail(ctx, bserrno);
4653 : 0 : return;
4654 : : }
4655 : :
4656 : 323 : bs_load_complete(ctx);
4657 : : }
4658 : :
4659 : : static void
4660 : 323 : bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4661 : : {
4662 : 323 : struct spdk_bs_load_ctx *ctx = cb_arg;
4663 : :
4664 : 323 : spdk_free(ctx->mask);
4665 : 323 : ctx->mask = NULL;
4666 : :
4667 [ - + ]: 323 : if (bserrno != 0) {
4668 : 0 : bs_load_ctx_fail(ctx, bserrno);
4669 : 0 : return;
4670 : : }
4671 : :
4672 : 323 : bs_write_used_clusters(seq, ctx, bs_load_write_used_clusters_cpl);
4673 : : }
4674 : :
4675 : : static void
4676 : 323 : bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4677 : : {
4678 : 323 : struct spdk_bs_load_ctx *ctx = cb_arg;
4679 : :
4680 : 323 : spdk_free(ctx->mask);
4681 : 323 : ctx->mask = NULL;
4682 : :
4683 [ - + ]: 323 : if (bserrno != 0) {
4684 : 0 : bs_load_ctx_fail(ctx, bserrno);
4685 : 0 : return;
4686 : : }
4687 : :
4688 : 323 : bs_write_used_blobids(seq, ctx, bs_load_write_used_blobids_cpl);
4689 : : }
4690 : :
4691 : : static void
4692 : 323 : bs_load_write_used_md(struct spdk_bs_load_ctx *ctx)
4693 : : {
4694 : 323 : bs_write_used_md(ctx->seq, ctx, bs_load_write_used_pages_cpl);
4695 : 323 : }
4696 : :
4697 : : static void
4698 : 22016 : bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx)
4699 : : {
4700 : : uint64_t num_md_clusters;
4701 : : uint64_t i;
4702 : :
4703 : 22016 : ctx->in_page_chain = false;
4704 : :
4705 : : do {
4706 : 22232 : ctx->page_index++;
4707 [ + + ]: 22232 : } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true);
4708 : :
4709 [ + + ]: 22016 : if (ctx->page_index < ctx->super->md_len) {
4710 : 21693 : ctx->cur_page = ctx->page_index;
4711 : 21693 : bs_load_replay_cur_md_page(ctx);
4712 : : } else {
4713 : : /* Claim all of the clusters used by the metadata */
4714 : 323 : num_md_clusters = spdk_divide_round_up(
4715 : 540 : ctx->super->md_start + ctx->super->md_len, ctx->bs->pages_per_cluster);
4716 [ + + ]: 1451 : for (i = 0; i < num_md_clusters; i++) {
4717 : 1128 : spdk_bit_array_set(ctx->used_clusters, i);
4718 : : }
4719 : 323 : ctx->bs->num_free_clusters -= num_md_clusters;
4720 : 323 : spdk_free(ctx->page);
4721 : 323 : bs_load_write_used_md(ctx);
4722 : : }
4723 : 22016 : }
4724 : :
4725 : : static void
4726 : 161 : bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4727 : : {
4728 : 161 : struct spdk_bs_load_ctx *ctx = cb_arg;
4729 : : uint32_t page_num;
4730 : : uint64_t i;
4731 : :
4732 [ - + ]: 161 : if (bserrno != 0) {
4733 : 0 : spdk_free(ctx->extent_pages);
4734 : 0 : bs_load_ctx_fail(ctx, bserrno);
4735 : 0 : return;
4736 : : }
4737 : :
4738 [ + + ]: 323 : for (i = 0; i < ctx->num_extent_pages; i++) {
4739 : : /* Extent pages are only read when present within in chain md.
4740 : : * Integrity of md is not right if that page was not a valid extent page. */
4741 [ - + ]: 162 : if (bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) {
4742 : 0 : spdk_free(ctx->extent_pages);
4743 : 0 : bs_load_ctx_fail(ctx, -EILSEQ);
4744 : 0 : return;
4745 : : }
4746 : :
4747 : 162 : page_num = ctx->extent_page_num[i];
4748 : 162 : spdk_bit_array_set(ctx->bs->used_md_pages, page_num);
4749 [ - + ]: 162 : if (bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) {
4750 : 0 : spdk_free(ctx->extent_pages);
4751 : 0 : bs_load_ctx_fail(ctx, -EILSEQ);
4752 : 0 : return;
4753 : : }
4754 : : }
4755 : :
4756 : 161 : spdk_free(ctx->extent_pages);
4757 : 161 : free(ctx->extent_page_num);
4758 : 161 : ctx->extent_page_num = NULL;
4759 : 161 : ctx->num_extent_pages = 0;
4760 : :
4761 : 161 : bs_load_replay_md_chain_cpl(ctx);
4762 : : }
4763 : :
4764 : : static void
4765 : 161 : bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx)
4766 : : {
4767 : : spdk_bs_batch_t *batch;
4768 : : uint32_t page;
4769 : : uint64_t lba;
4770 : : uint64_t i;
4771 : :
4772 : 161 : ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, 0,
4773 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4774 [ - + ]: 161 : if (!ctx->extent_pages) {
4775 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4776 : 0 : return;
4777 : : }
4778 : :
4779 : 161 : batch = bs_sequence_to_batch(ctx->seq, bs_load_replay_extent_page_cpl, ctx);
4780 : :
4781 [ + + ]: 323 : for (i = 0; i < ctx->num_extent_pages; i++) {
4782 : 162 : page = ctx->extent_page_num[i];
4783 [ - + ]: 162 : assert(page < ctx->super->md_len);
4784 : 162 : lba = bs_md_page_to_lba(ctx->bs, page);
4785 : 162 : bs_batch_read_dev(batch, &ctx->extent_pages[i], lba,
4786 : 162 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE));
4787 : : }
4788 : :
4789 : 161 : bs_batch_close(batch);
4790 : : }
4791 : :
4792 : : static void
4793 : 22136 : bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4794 : : {
4795 : 22136 : struct spdk_bs_load_ctx *ctx = cb_arg;
4796 : : uint32_t page_num;
4797 : : struct spdk_blob_md_page *page;
4798 : :
4799 [ - + ]: 22136 : if (bserrno != 0) {
4800 : 0 : bs_load_ctx_fail(ctx, bserrno);
4801 : 0 : return;
4802 : : }
4803 : :
4804 : 22136 : page_num = ctx->cur_page;
4805 : 22136 : page = ctx->page;
4806 [ + + ]: 22136 : if (bs_load_cur_md_page_valid(ctx) == true) {
4807 [ + + + + : 604 : if (page->sequence_num == 0 || ctx->in_page_chain == true) {
+ + ]
4808 : 592 : spdk_spin_lock(&ctx->bs->used_lock);
4809 : 592 : bs_claim_md_page(ctx->bs, page_num);
4810 : 592 : spdk_spin_unlock(&ctx->bs->used_lock);
4811 [ + + ]: 592 : if (page->sequence_num == 0) {
4812 : 472 : SPDK_NOTICELOG("Recover: blob 0x%" PRIx32 "\n", page_num);
4813 : 472 : spdk_bit_array_set(ctx->bs->used_blobids, page_num);
4814 : : }
4815 [ - + ]: 592 : if (bs_load_replay_md_parse_page(ctx, page)) {
4816 : 0 : bs_load_ctx_fail(ctx, -EILSEQ);
4817 : 0 : return;
4818 : : }
4819 [ + + ]: 592 : if (page->next != SPDK_INVALID_MD_PAGE) {
4820 : 120 : ctx->in_page_chain = true;
4821 : 120 : ctx->cur_page = page->next;
4822 : 120 : bs_load_replay_cur_md_page(ctx);
4823 : 120 : return;
4824 : : }
4825 [ + + ]: 472 : if (ctx->num_extent_pages != 0) {
4826 : 161 : bs_load_replay_extent_pages(ctx);
4827 : 161 : return;
4828 : : }
4829 : : }
4830 : : }
4831 : 21855 : bs_load_replay_md_chain_cpl(ctx);
4832 : : }
4833 : :
4834 : : static void
4835 : 22136 : bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx)
4836 : : {
4837 : : uint64_t lba;
4838 : :
4839 [ - + ]: 22136 : assert(ctx->cur_page < ctx->super->md_len);
4840 : 22136 : lba = bs_md_page_to_lba(ctx->bs, ctx->cur_page);
4841 : 22136 : bs_sequence_read_dev(ctx->seq, ctx->page, lba,
4842 : 22136 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
4843 : : bs_load_replay_md_cpl, ctx);
4844 : 22136 : }
4845 : :
4846 : : static void
4847 : 323 : bs_load_replay_md(struct spdk_bs_load_ctx *ctx)
4848 : : {
4849 : 323 : ctx->page_index = 0;
4850 : 323 : ctx->cur_page = 0;
4851 : 323 : ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
4852 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4853 [ - + ]: 323 : if (!ctx->page) {
4854 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4855 : 0 : return;
4856 : : }
4857 : 323 : bs_load_replay_cur_md_page(ctx);
4858 : : }
4859 : :
4860 : : static void
4861 : 323 : bs_recover(struct spdk_bs_load_ctx *ctx)
4862 : : {
4863 : : int rc;
4864 : :
4865 : 323 : SPDK_NOTICELOG("Performing recovery on blobstore\n");
4866 : 323 : rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len);
4867 [ - + ]: 323 : if (rc < 0) {
4868 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4869 : 0 : return;
4870 : : }
4871 : :
4872 : 323 : rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len);
4873 [ - + ]: 323 : if (rc < 0) {
4874 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4875 : 0 : return;
4876 : : }
4877 : :
4878 : 323 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
4879 [ - + ]: 323 : if (rc < 0) {
4880 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4881 : 0 : return;
4882 : : }
4883 : :
4884 : 323 : rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->super->md_len);
4885 [ - + ]: 323 : if (rc < 0) {
4886 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4887 : 0 : return;
4888 : : }
4889 : :
4890 : 323 : ctx->bs->num_free_clusters = ctx->bs->total_clusters;
4891 : 323 : bs_load_replay_md(ctx);
4892 : : }
4893 : :
4894 : : static int
4895 : 911 : bs_parse_super(struct spdk_bs_load_ctx *ctx)
4896 : : {
4897 : : int rc;
4898 : :
4899 [ + + ]: 911 : if (ctx->super->size == 0) {
4900 : 24 : ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
4901 : : }
4902 : :
4903 [ + + ]: 911 : if (ctx->super->io_unit_size == 0) {
4904 : 24 : ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
4905 : : }
4906 : :
4907 : 911 : ctx->bs->clean = 1;
4908 : 911 : ctx->bs->cluster_sz = ctx->super->cluster_size;
4909 [ - + ]: 911 : ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
4910 : 911 : ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
4911 [ + - ]: 911 : if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
4912 : 911 : ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
4913 : : }
4914 : 911 : ctx->bs->io_unit_size = ctx->super->io_unit_size;
4915 : 911 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
4916 [ - + ]: 911 : if (rc < 0) {
4917 : 0 : return -ENOMEM;
4918 : : }
4919 : 911 : ctx->bs->md_start = ctx->super->md_start;
4920 : 911 : ctx->bs->md_len = ctx->super->md_len;
4921 : 911 : rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
4922 [ - + ]: 911 : if (rc < 0) {
4923 : 0 : return -ENOMEM;
4924 : : }
4925 : :
4926 : 2454 : ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
4927 : 1543 : ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
4928 : 911 : ctx->bs->super_blob = ctx->super->super_blob;
4929 : 911 : memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
4930 : :
4931 : 911 : return 0;
4932 : : }
4933 : :
4934 : : static void
4935 : 7411 : bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4936 : : {
4937 : 7411 : struct spdk_bs_load_ctx *ctx = cb_arg;
4938 : : int rc;
4939 : :
4940 : 7411 : rc = bs_super_validate(ctx->super, ctx->bs);
4941 [ + + ]: 7411 : if (rc != 0) {
4942 : 6500 : bs_load_ctx_fail(ctx, rc);
4943 : 6500 : return;
4944 : : }
4945 : :
4946 : 911 : rc = bs_parse_super(ctx);
4947 [ - + ]: 911 : if (rc < 0) {
4948 : 0 : bs_load_ctx_fail(ctx, rc);
4949 : 0 : return;
4950 : : }
4951 : :
4952 [ + + + + : 911 : if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0 || ctx->force_recover) {
- + - + ]
4953 : 323 : bs_recover(ctx);
4954 : : } else {
4955 : 588 : bs_load_read_used_pages(ctx);
4956 : : }
4957 : : }
4958 : :
4959 : : static inline int
4960 : 7618 : bs_opts_copy(struct spdk_bs_opts *src, struct spdk_bs_opts *dst)
4961 : : {
4962 : :
4963 [ - + ]: 7618 : if (!src->opts_size) {
4964 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
4965 : 0 : return -1;
4966 : : }
4967 : :
4968 : : #define FIELD_OK(field) \
4969 : : offsetof(struct spdk_bs_opts, field) + sizeof(src->field) <= src->opts_size
4970 : :
4971 : : #define SET_FIELD(field) \
4972 : : if (FIELD_OK(field)) { \
4973 : : dst->field = src->field; \
4974 : : } \
4975 : :
4976 [ + - ]: 7618 : SET_FIELD(cluster_sz);
4977 [ + - ]: 7618 : SET_FIELD(num_md_pages);
4978 [ + - ]: 7618 : SET_FIELD(max_md_ops);
4979 [ + - ]: 7618 : SET_FIELD(max_channel_ops);
4980 [ + - ]: 7618 : SET_FIELD(clear_method);
4981 : :
4982 [ + - ]: 7618 : if (FIELD_OK(bstype)) {
4983 : 7618 : memcpy(&dst->bstype, &src->bstype, sizeof(dst->bstype));
4984 : : }
4985 [ + - ]: 7618 : SET_FIELD(iter_cb_fn);
4986 [ + - ]: 7618 : SET_FIELD(iter_cb_arg);
4987 [ + - ]: 7618 : SET_FIELD(force_recover);
4988 [ + - ]: 7618 : SET_FIELD(esnap_bs_dev_create);
4989 [ + - ]: 7618 : SET_FIELD(esnap_ctx);
4990 : :
4991 : 7618 : dst->opts_size = src->opts_size;
4992 : :
4993 : : /* You should not remove this statement, but need to update the assert statement
4994 : : * if you add a new field, and also add a corresponding SET_FIELD statement */
4995 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_opts) == 88, "Incorrect size");
4996 : :
4997 : : #undef FIELD_OK
4998 : : #undef SET_FIELD
4999 : :
5000 : 7618 : return 0;
5001 : : }
5002 : :
5003 : : void
5004 : 7493 : spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
5005 : : spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
5006 : : {
5007 : 4755 : struct spdk_blob_store *bs;
5008 : 4755 : struct spdk_bs_cpl cpl;
5009 : 4755 : struct spdk_bs_load_ctx *ctx;
5010 : 7493 : struct spdk_bs_opts opts = {};
5011 : : int err;
5012 : :
5013 [ - + - + ]: 7493 : SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
5014 : :
5015 [ + + + + ]: 7493 : if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
5016 [ - + - + ]: 12 : SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
5017 : 12 : dev->destroy(dev);
5018 : 12 : cb_fn(cb_arg, NULL, -EINVAL);
5019 : 34 : return;
5020 : : }
5021 : :
5022 : 7481 : spdk_bs_opts_init(&opts, sizeof(opts));
5023 [ + + ]: 7481 : if (o) {
5024 [ - + ]: 6912 : if (bs_opts_copy(o, &opts)) {
5025 : 0 : return;
5026 : : }
5027 : : }
5028 : :
5029 [ + + + + ]: 7481 : if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
5030 : 24 : dev->destroy(dev);
5031 : 24 : cb_fn(cb_arg, NULL, -EINVAL);
5032 : 24 : return;
5033 : : }
5034 : :
5035 : 7457 : err = bs_alloc(dev, &opts, &bs, &ctx);
5036 [ + + ]: 7457 : if (err) {
5037 : 46 : dev->destroy(dev);
5038 : 46 : cb_fn(cb_arg, NULL, err);
5039 : 46 : return;
5040 : : }
5041 : :
5042 : 7411 : cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
5043 : 7411 : cpl.u.bs_handle.cb_fn = cb_fn;
5044 : 7411 : cpl.u.bs_handle.cb_arg = cb_arg;
5045 : 7411 : cpl.u.bs_handle.bs = bs;
5046 : :
5047 : 7411 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5048 [ - + ]: 7411 : if (!ctx->seq) {
5049 : 0 : spdk_free(ctx->super);
5050 : 0 : free(ctx);
5051 : 0 : bs_free(bs);
5052 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5053 : 0 : return;
5054 : : }
5055 : :
5056 : : /* Read the super block */
5057 : 7411 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
5058 : 7411 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
5059 : : bs_load_super_cpl, ctx);
5060 : : }
5061 : :
5062 : : /* END spdk_bs_load */
5063 : :
5064 : : /* START spdk_bs_dump */
5065 : :
5066 : : static void
5067 : 0 : bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno)
5068 : : {
5069 : 0 : spdk_free(ctx->super);
5070 : :
5071 : : /*
5072 : : * We need to defer calling bs_call_cpl() until after
5073 : : * dev destruction, so tuck these away for later use.
5074 : : */
5075 : 0 : ctx->bs->unload_err = bserrno;
5076 [ # # # # ]: 0 : memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
5077 : 0 : seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
5078 : :
5079 : 0 : bs_sequence_finish(seq, 0);
5080 : 0 : bs_free(ctx->bs);
5081 : 0 : free(ctx);
5082 : 0 : }
5083 : :
5084 : : static void
5085 : 0 : bs_dump_print_xattr(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
5086 : : {
5087 : : struct spdk_blob_md_descriptor_xattr *desc_xattr;
5088 : : uint32_t i;
5089 : : const char *type;
5090 : :
5091 : 0 : desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc;
5092 : :
5093 : 0 : if (desc_xattr->length !=
5094 : : sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) +
5095 : 0 : desc_xattr->name_length + desc_xattr->value_length) {
5096 : : }
5097 : :
5098 [ # # # # ]: 0 : memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length);
5099 : 0 : ctx->xattr_name[desc_xattr->name_length] = '\0';
5100 [ # # ]: 0 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
5101 : 0 : type = "XATTR";
5102 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
5103 : 0 : type = "XATTR_INTERNAL";
5104 : : } else {
5105 : 0 : assert(false);
5106 : : type = "XATTR_?";
5107 : : }
5108 [ # # # # ]: 0 : fprintf(ctx->fp, "%s: name = \"%s\"\n", type, ctx->xattr_name);
5109 [ # # # # ]: 0 : fprintf(ctx->fp, " value = \"");
5110 : 0 : ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name,
5111 : 0 : (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
5112 : 0 : desc_xattr->value_length);
5113 [ # # # # ]: 0 : fprintf(ctx->fp, "\"\n");
5114 [ # # ]: 0 : for (i = 0; i < desc_xattr->value_length; i++) {
5115 [ # # ]: 0 : if (i % 16 == 0) {
5116 [ # # # # ]: 0 : fprintf(ctx->fp, " ");
5117 : : }
5118 [ # # # # ]: 0 : fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i));
5119 [ # # ]: 0 : if ((i + 1) % 16 == 0) {
5120 [ # # ]: 0 : fprintf(ctx->fp, "\n");
5121 : : }
5122 : : }
5123 [ # # ]: 0 : if (i % 16 != 0) {
5124 [ # # ]: 0 : fprintf(ctx->fp, "\n");
5125 : : }
5126 : 0 : }
5127 : :
5128 : : struct type_flag_desc {
5129 : : uint64_t mask;
5130 : : uint64_t val;
5131 : : const char *name;
5132 : : };
5133 : :
5134 : : static void
5135 : 0 : bs_dump_print_type_bits(struct spdk_bs_load_ctx *ctx, uint64_t flags,
5136 : : struct type_flag_desc *desc, size_t numflags)
5137 : : {
5138 : 0 : uint64_t covered = 0;
5139 : : size_t i;
5140 : :
5141 [ # # ]: 0 : for (i = 0; i < numflags; i++) {
5142 [ # # ]: 0 : if ((desc[i].mask & flags) != desc[i].val) {
5143 : 0 : continue;
5144 : : }
5145 [ # # # # ]: 0 : fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " %s", desc[i].val, desc[i].name);
5146 [ # # ]: 0 : if (desc[i].mask != desc[i].val) {
5147 [ # # # # ]: 0 : fprintf(ctx->fp, " (mask 0x%" PRIx64 " value 0x%" PRIx64 ")",
5148 : 0 : desc[i].mask, desc[i].val);
5149 : : }
5150 [ # # ]: 0 : fprintf(ctx->fp, "\n");
5151 : 0 : covered |= desc[i].mask;
5152 : : }
5153 [ # # ]: 0 : if ((flags & ~covered) != 0) {
5154 [ # # # # ]: 0 : fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " Unknown\n", flags & ~covered);
5155 : : }
5156 : 0 : }
5157 : :
5158 : : static void
5159 : 0 : bs_dump_print_type_flags(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
5160 : : {
5161 : : struct spdk_blob_md_descriptor_flags *type_desc;
5162 : : #define ADD_FLAG(f) { f, f, #f }
5163 : : #define ADD_MASK_VAL(m, v) { m, v, #v }
5164 : : static struct type_flag_desc invalid[] = {
5165 : : ADD_FLAG(SPDK_BLOB_THIN_PROV),
5166 : : ADD_FLAG(SPDK_BLOB_INTERNAL_XATTR),
5167 : : ADD_FLAG(SPDK_BLOB_EXTENT_TABLE),
5168 : : };
5169 : : static struct type_flag_desc data_ro[] = {
5170 : : ADD_FLAG(SPDK_BLOB_READ_ONLY),
5171 : : };
5172 : : static struct type_flag_desc md_ro[] = {
5173 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_DEFAULT),
5174 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_NONE),
5175 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_UNMAP),
5176 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_WRITE_ZEROES),
5177 : : };
5178 : : #undef ADD_FLAG
5179 : : #undef ADD_MASK_VAL
5180 : :
5181 : 0 : type_desc = (struct spdk_blob_md_descriptor_flags *)desc;
5182 [ # # # # ]: 0 : fprintf(ctx->fp, "Flags:\n");
5183 [ # # # # ]: 0 : fprintf(ctx->fp, "\tinvalid: 0x%016" PRIx64 "\n", type_desc->invalid_flags);
5184 : 0 : bs_dump_print_type_bits(ctx, type_desc->invalid_flags, invalid,
5185 : : SPDK_COUNTOF(invalid));
5186 [ # # # # ]: 0 : fprintf(ctx->fp, "\tdata_ro: 0x%016" PRIx64 "\n", type_desc->data_ro_flags);
5187 : 0 : bs_dump_print_type_bits(ctx, type_desc->data_ro_flags, data_ro,
5188 : : SPDK_COUNTOF(data_ro));
5189 [ # # # # ]: 0 : fprintf(ctx->fp, "\t md_ro: 0x%016" PRIx64 "\n", type_desc->md_ro_flags);
5190 : 0 : bs_dump_print_type_bits(ctx, type_desc->md_ro_flags, md_ro,
5191 : : SPDK_COUNTOF(md_ro));
5192 : 0 : }
5193 : :
5194 : : static void
5195 : 0 : bs_dump_print_extent_table(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
5196 : : {
5197 : : struct spdk_blob_md_descriptor_extent_table *et_desc;
5198 : : uint64_t num_extent_pages;
5199 : : uint32_t et_idx;
5200 : :
5201 : 0 : et_desc = (struct spdk_blob_md_descriptor_extent_table *)desc;
5202 : 0 : num_extent_pages = (et_desc->length - sizeof(et_desc->num_clusters)) /
5203 : : sizeof(et_desc->extent_page[0]);
5204 : :
5205 [ # # # # ]: 0 : fprintf(ctx->fp, "Extent table:\n");
5206 [ # # ]: 0 : for (et_idx = 0; et_idx < num_extent_pages; et_idx++) {
5207 [ # # ]: 0 : if (et_desc->extent_page[et_idx].page_idx == 0) {
5208 : : /* Zeroes represent unallocated extent pages. */
5209 : 0 : continue;
5210 : : }
5211 [ # # # # ]: 0 : fprintf(ctx->fp, "\tExtent page: %5" PRIu32 " length %3" PRIu32
5212 : : " at LBA %" PRIu64 "\n", et_desc->extent_page[et_idx].page_idx,
5213 : : et_desc->extent_page[et_idx].num_pages,
5214 : : bs_md_page_to_lba(ctx->bs, et_desc->extent_page[et_idx].page_idx));
5215 : : }
5216 : 0 : }
5217 : :
5218 : : static void
5219 : 0 : bs_dump_print_md_page(struct spdk_bs_load_ctx *ctx)
5220 : : {
5221 : 0 : uint32_t page_idx = ctx->cur_page;
5222 : 0 : struct spdk_blob_md_page *page = ctx->page;
5223 : : struct spdk_blob_md_descriptor *desc;
5224 : 0 : size_t cur_desc = 0;
5225 : : uint32_t crc;
5226 : :
5227 [ # # # # ]: 0 : fprintf(ctx->fp, "=========\n");
5228 [ # # # # ]: 0 : fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx);
5229 [ # # # # ]: 0 : fprintf(ctx->fp, "Start LBA: %" PRIu64 "\n", bs_md_page_to_lba(ctx->bs, page_idx));
5230 [ # # # # ]: 0 : fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id);
5231 [ # # # # ]: 0 : fprintf(ctx->fp, "Sequence: %" PRIu32 "\n", page->sequence_num);
5232 [ # # ]: 0 : if (page->next == SPDK_INVALID_MD_PAGE) {
5233 [ # # # # ]: 0 : fprintf(ctx->fp, "Next: None\n");
5234 : : } else {
5235 [ # # # # ]: 0 : fprintf(ctx->fp, "Next: %" PRIu32 "\n", page->next);
5236 : : }
5237 [ # # # # : 0 : fprintf(ctx->fp, "In used bit array%s:", ctx->super->clean ? "" : " (not clean: dubious)");
# # ]
5238 [ # # ]: 0 : if (spdk_bit_array_get(ctx->bs->used_md_pages, page_idx)) {
5239 [ # # # # ]: 0 : fprintf(ctx->fp, " md");
5240 : : }
5241 [ # # ]: 0 : if (spdk_bit_array_get(ctx->bs->used_blobids, page_idx)) {
5242 [ # # # # ]: 0 : fprintf(ctx->fp, " blob");
5243 : : }
5244 [ # # ]: 0 : fprintf(ctx->fp, "\n");
5245 : :
5246 : 0 : crc = blob_md_page_calc_crc(page);
5247 [ # # # # : 0 : fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch");
# # ]
5248 : :
5249 : 0 : desc = (struct spdk_blob_md_descriptor *)page->descriptors;
5250 [ # # ]: 0 : while (cur_desc < sizeof(page->descriptors)) {
5251 [ # # ]: 0 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
5252 [ # # ]: 0 : if (desc->length == 0) {
5253 : : /* If padding and length are 0, this terminates the page */
5254 : 0 : break;
5255 : : }
5256 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
5257 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
5258 : : unsigned int i;
5259 : :
5260 : 0 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
5261 : :
5262 [ # # ]: 0 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
5263 [ # # ]: 0 : if (desc_extent_rle->extents[i].cluster_idx != 0) {
5264 [ # # # # ]: 0 : fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
5265 : : desc_extent_rle->extents[i].cluster_idx);
5266 : : } else {
5267 [ # # # # ]: 0 : fprintf(ctx->fp, "Unallocated Extent - ");
5268 : : }
5269 [ # # # # ]: 0 : fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length);
5270 [ # # ]: 0 : fprintf(ctx->fp, "\n");
5271 : : }
5272 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
5273 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
5274 : : unsigned int i;
5275 : :
5276 : 0 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
5277 : :
5278 [ # # ]: 0 : for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) {
5279 [ # # ]: 0 : if (desc_extent->cluster_idx[i] != 0) {
5280 [ # # # # ]: 0 : fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
5281 : : desc_extent->cluster_idx[i]);
5282 : : } else {
5283 [ # # # # ]: 0 : fprintf(ctx->fp, "Unallocated Extent");
5284 : : }
5285 [ # # ]: 0 : fprintf(ctx->fp, "\n");
5286 : : }
5287 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
5288 : 0 : bs_dump_print_xattr(ctx, desc);
5289 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
5290 : 0 : bs_dump_print_xattr(ctx, desc);
5291 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
5292 : 0 : bs_dump_print_type_flags(ctx, desc);
5293 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
5294 : 0 : bs_dump_print_extent_table(ctx, desc);
5295 : : } else {
5296 : : /* Error */
5297 [ # # # # ]: 0 : fprintf(ctx->fp, "Unknown descriptor type %" PRIu8 "\n", desc->type);
5298 : : }
5299 : : /* Advance to the next descriptor */
5300 : 0 : cur_desc += sizeof(*desc) + desc->length;
5301 [ # # ]: 0 : if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
5302 : 0 : break;
5303 : : }
5304 : 0 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
5305 : : }
5306 : 0 : }
5307 : :
5308 : : static void
5309 : 0 : bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5310 : : {
5311 : 0 : struct spdk_bs_load_ctx *ctx = cb_arg;
5312 : :
5313 [ # # ]: 0 : if (bserrno != 0) {
5314 : 0 : bs_dump_finish(seq, ctx, bserrno);
5315 : 0 : return;
5316 : : }
5317 : :
5318 [ # # ]: 0 : if (ctx->page->id != 0) {
5319 : 0 : bs_dump_print_md_page(ctx);
5320 : : }
5321 : :
5322 : 0 : ctx->cur_page++;
5323 : :
5324 [ # # ]: 0 : if (ctx->cur_page < ctx->super->md_len) {
5325 : 0 : bs_dump_read_md_page(seq, ctx);
5326 : : } else {
5327 : 0 : spdk_free(ctx->page);
5328 : 0 : bs_dump_finish(seq, ctx, 0);
5329 : : }
5330 : : }
5331 : :
5332 : : static void
5333 : 0 : bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg)
5334 : : {
5335 : 0 : struct spdk_bs_load_ctx *ctx = cb_arg;
5336 : : uint64_t lba;
5337 : :
5338 [ # # ]: 0 : assert(ctx->cur_page < ctx->super->md_len);
5339 : 0 : lba = bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page);
5340 : 0 : bs_sequence_read_dev(seq, ctx->page, lba,
5341 : 0 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
5342 : : bs_dump_read_md_page_cpl, ctx);
5343 : 0 : }
5344 : :
5345 : : static void
5346 : 0 : bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5347 : : {
5348 : 0 : struct spdk_bs_load_ctx *ctx = cb_arg;
5349 : : int rc;
5350 : :
5351 [ # # ]: 0 : fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature);
5352 [ # # # # ]: 0 : if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
5353 : : sizeof(ctx->super->signature)) != 0) {
5354 [ # # ]: 0 : fprintf(ctx->fp, "(Mismatch)\n");
5355 : 0 : bs_dump_finish(seq, ctx, bserrno);
5356 : 0 : return;
5357 : : } else {
5358 [ # # ]: 0 : fprintf(ctx->fp, "(OK)\n");
5359 : : }
5360 [ # # ]: 0 : fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version);
5361 [ # # # # ]: 0 : fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc,
5362 : 0 : (ctx->super->crc == blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch");
5363 [ # # ]: 0 : fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype);
5364 [ # # ]: 0 : fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size);
5365 [ # # ]: 0 : fprintf(ctx->fp, "Super Blob ID: ");
5366 [ # # ]: 0 : if (ctx->super->super_blob == SPDK_BLOBID_INVALID) {
5367 [ # # ]: 0 : fprintf(ctx->fp, "(None)\n");
5368 : : } else {
5369 [ # # ]: 0 : fprintf(ctx->fp, "0x%" PRIx64 "\n", ctx->super->super_blob);
5370 : : }
5371 [ # # ]: 0 : fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean);
5372 [ # # ]: 0 : fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start);
5373 [ # # ]: 0 : fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len);
5374 [ # # ]: 0 : fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start);
5375 [ # # ]: 0 : fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len);
5376 [ # # ]: 0 : fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start);
5377 [ # # ]: 0 : fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len);
5378 [ # # ]: 0 : fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start);
5379 [ # # ]: 0 : fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len);
5380 : :
5381 : 0 : ctx->cur_page = 0;
5382 : 0 : ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
5383 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
5384 [ # # ]: 0 : if (!ctx->page) {
5385 : 0 : bs_dump_finish(seq, ctx, -ENOMEM);
5386 : 0 : return;
5387 : : }
5388 : :
5389 : 0 : rc = bs_parse_super(ctx);
5390 [ # # ]: 0 : if (rc < 0) {
5391 : 0 : bs_load_ctx_fail(ctx, rc);
5392 : 0 : return;
5393 : : }
5394 : :
5395 : 0 : bs_load_read_used_pages(ctx);
5396 : : }
5397 : :
5398 : : void
5399 : 0 : spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn,
5400 : : spdk_bs_op_complete cb_fn, void *cb_arg)
5401 : : {
5402 : 0 : struct spdk_blob_store *bs;
5403 : 0 : struct spdk_bs_cpl cpl;
5404 : 0 : struct spdk_bs_load_ctx *ctx;
5405 : 0 : struct spdk_bs_opts opts = {};
5406 : : int err;
5407 : :
5408 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "Dumping blobstore from dev %p\n", dev);
5409 : :
5410 : 0 : spdk_bs_opts_init(&opts, sizeof(opts));
5411 : :
5412 : 0 : err = bs_alloc(dev, &opts, &bs, &ctx);
5413 [ # # ]: 0 : if (err) {
5414 : 0 : dev->destroy(dev);
5415 : 0 : cb_fn(cb_arg, err);
5416 : 0 : return;
5417 : : }
5418 : :
5419 : 0 : ctx->dumping = true;
5420 : 0 : ctx->fp = fp;
5421 : 0 : ctx->print_xattr_fn = print_xattr_fn;
5422 : :
5423 : 0 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5424 : 0 : cpl.u.bs_basic.cb_fn = cb_fn;
5425 : 0 : cpl.u.bs_basic.cb_arg = cb_arg;
5426 : :
5427 : 0 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5428 [ # # ]: 0 : if (!ctx->seq) {
5429 : 0 : spdk_free(ctx->super);
5430 : 0 : free(ctx);
5431 : 0 : bs_free(bs);
5432 : 0 : cb_fn(cb_arg, -ENOMEM);
5433 : 0 : return;
5434 : : }
5435 : :
5436 : : /* Read the super block */
5437 : 0 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
5438 : 0 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
5439 : : bs_dump_super_cpl, ctx);
5440 : : }
5441 : :
5442 : : /* END spdk_bs_dump */
5443 : :
5444 : : /* START spdk_bs_init */
5445 : :
5446 : : static void
5447 : 1565 : bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5448 : : {
5449 : 1565 : struct spdk_bs_load_ctx *ctx = cb_arg;
5450 : :
5451 : 1565 : ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
5452 : 1565 : spdk_free(ctx->super);
5453 : 1565 : free(ctx);
5454 : :
5455 : 1565 : bs_sequence_finish(seq, bserrno);
5456 : 1565 : }
5457 : :
5458 : : static void
5459 : 1565 : bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5460 : : {
5461 : 1565 : struct spdk_bs_load_ctx *ctx = cb_arg;
5462 : :
5463 : : /* Write super block */
5464 : 1565 : bs_sequence_write_dev(seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
5465 : 1565 : bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
5466 : : bs_init_persist_super_cpl, ctx);
5467 : 1565 : }
5468 : :
5469 : : void
5470 : 1614 : spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
5471 : : spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
5472 : : {
5473 : 1578 : struct spdk_bs_load_ctx *ctx;
5474 : 1578 : struct spdk_blob_store *bs;
5475 : 1578 : struct spdk_bs_cpl cpl;
5476 : : spdk_bs_sequence_t *seq;
5477 : : spdk_bs_batch_t *batch;
5478 : : uint64_t num_md_lba;
5479 : : uint64_t num_md_pages;
5480 : : uint64_t num_md_clusters;
5481 : : uint64_t max_used_cluster_mask_len;
5482 : : uint32_t i;
5483 : 1614 : struct spdk_bs_opts opts = {};
5484 : : int rc;
5485 : : uint64_t lba, lba_count;
5486 : :
5487 [ - + - + ]: 1614 : SPDK_DEBUGLOG(blob, "Initializing blobstore on dev %p\n", dev);
5488 : :
5489 [ + + + + ]: 1614 : if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
5490 : 12 : SPDK_ERRLOG("unsupported dev block length of %d\n",
5491 : : dev->blocklen);
5492 : 12 : dev->destroy(dev);
5493 : 12 : cb_fn(cb_arg, NULL, -EINVAL);
5494 : 12 : return;
5495 : : }
5496 : :
5497 : 1602 : spdk_bs_opts_init(&opts, sizeof(opts));
5498 [ + + ]: 1602 : if (o) {
5499 [ - + ]: 694 : if (bs_opts_copy(o, &opts)) {
5500 : 0 : return;
5501 : : }
5502 : : }
5503 : :
5504 [ + + ]: 1602 : if (bs_opts_verify(&opts) != 0) {
5505 : 12 : dev->destroy(dev);
5506 : 12 : cb_fn(cb_arg, NULL, -EINVAL);
5507 : 12 : return;
5508 : : }
5509 : :
5510 : 1590 : rc = bs_alloc(dev, &opts, &bs, &ctx);
5511 [ + + ]: 1590 : if (rc) {
5512 : 12 : dev->destroy(dev);
5513 : 12 : cb_fn(cb_arg, NULL, rc);
5514 : 12 : return;
5515 : : }
5516 : :
5517 [ + + ]: 1578 : if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
5518 : : /* By default, allocate 1 page per cluster.
5519 : : * Technically, this over-allocates metadata
5520 : : * because more metadata will reduce the number
5521 : : * of usable clusters. This can be addressed with
5522 : : * more complex math in the future.
5523 : : */
5524 : 1409 : bs->md_len = bs->total_clusters;
5525 : : } else {
5526 : 169 : bs->md_len = opts.num_md_pages;
5527 : : }
5528 : 1578 : rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
5529 [ - + ]: 1578 : if (rc < 0) {
5530 : 0 : spdk_free(ctx->super);
5531 : 0 : free(ctx);
5532 : 0 : bs_free(bs);
5533 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5534 : 0 : return;
5535 : : }
5536 : :
5537 : 1578 : rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len);
5538 [ - + ]: 1578 : if (rc < 0) {
5539 : 0 : spdk_free(ctx->super);
5540 : 0 : free(ctx);
5541 : 0 : bs_free(bs);
5542 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5543 : 0 : return;
5544 : : }
5545 : :
5546 : 1578 : rc = spdk_bit_array_resize(&bs->open_blobids, bs->md_len);
5547 [ - + ]: 1578 : if (rc < 0) {
5548 : 0 : spdk_free(ctx->super);
5549 : 0 : free(ctx);
5550 : 0 : bs_free(bs);
5551 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5552 : 0 : return;
5553 : : }
5554 : :
5555 [ - + - + ]: 1578 : memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
5556 : : sizeof(ctx->super->signature));
5557 : 1578 : ctx->super->version = SPDK_BS_VERSION;
5558 : 1578 : ctx->super->length = sizeof(*ctx->super);
5559 : 1578 : ctx->super->super_blob = bs->super_blob;
5560 : 1578 : ctx->super->clean = 0;
5561 : 1578 : ctx->super->cluster_size = bs->cluster_sz;
5562 : 1578 : ctx->super->io_unit_size = bs->io_unit_size;
5563 : 1578 : memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
5564 : :
5565 : : /* Calculate how many pages the metadata consumes at the front
5566 : : * of the disk.
5567 : : */
5568 : :
5569 : : /* The super block uses 1 page */
5570 : 1578 : num_md_pages = 1;
5571 : :
5572 : : /* The used_md_pages mask requires 1 bit per metadata page, rounded
5573 : : * up to the nearest page, plus a header.
5574 : : */
5575 : 1578 : ctx->super->used_page_mask_start = num_md_pages;
5576 : 1578 : ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5577 : 1578 : spdk_divide_round_up(bs->md_len, 8),
5578 : : SPDK_BS_PAGE_SIZE);
5579 : 1578 : num_md_pages += ctx->super->used_page_mask_len;
5580 : :
5581 : : /* The used_clusters mask requires 1 bit per cluster, rounded
5582 : : * up to the nearest page, plus a header.
5583 : : */
5584 : 1578 : ctx->super->used_cluster_mask_start = num_md_pages;
5585 : 1578 : ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5586 : 1578 : spdk_divide_round_up(bs->total_clusters, 8),
5587 : : SPDK_BS_PAGE_SIZE);
5588 : : /* The blobstore might be extended, then the used_cluster bitmap will need more space.
5589 : : * Here we calculate the max clusters we can support according to the
5590 : : * num_md_pages (bs->md_len).
5591 : : */
5592 : 1578 : max_used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5593 : 1578 : spdk_divide_round_up(bs->md_len, 8),
5594 : : SPDK_BS_PAGE_SIZE);
5595 : 1578 : max_used_cluster_mask_len = spdk_max(max_used_cluster_mask_len,
5596 : : ctx->super->used_cluster_mask_len);
5597 : 1578 : num_md_pages += max_used_cluster_mask_len;
5598 : :
5599 : : /* The used_blobids mask requires 1 bit per metadata page, rounded
5600 : : * up to the nearest page, plus a header.
5601 : : */
5602 : 1578 : ctx->super->used_blobid_mask_start = num_md_pages;
5603 : 1578 : ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5604 : 1578 : spdk_divide_round_up(bs->md_len, 8),
5605 : : SPDK_BS_PAGE_SIZE);
5606 : 1578 : num_md_pages += ctx->super->used_blobid_mask_len;
5607 : :
5608 : : /* The metadata region size was chosen above */
5609 : 1578 : ctx->super->md_start = bs->md_start = num_md_pages;
5610 : 1578 : ctx->super->md_len = bs->md_len;
5611 : 1578 : num_md_pages += bs->md_len;
5612 : :
5613 : 1578 : num_md_lba = bs_page_to_lba(bs, num_md_pages);
5614 : :
5615 : 1578 : ctx->super->size = dev->blockcnt * dev->blocklen;
5616 : :
5617 : 1578 : ctx->super->crc = blob_md_page_calc_crc(ctx->super);
5618 : :
5619 : 1578 : num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster);
5620 [ + + ]: 1578 : if (num_md_clusters > bs->total_clusters) {
5621 : 13 : SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
5622 : : "please decrease number of pages reserved for metadata "
5623 : : "or increase cluster size.\n");
5624 : 13 : spdk_free(ctx->super);
5625 : 13 : spdk_bit_array_free(&ctx->used_clusters);
5626 : 13 : free(ctx);
5627 : 13 : bs_free(bs);
5628 : 13 : cb_fn(cb_arg, NULL, -ENOMEM);
5629 : 13 : return;
5630 : : }
5631 : : /* Claim all of the clusters used by the metadata */
5632 [ + + ]: 283129 : for (i = 0; i < num_md_clusters; i++) {
5633 : 281564 : spdk_bit_array_set(ctx->used_clusters, i);
5634 : : }
5635 : :
5636 : 1565 : bs->num_free_clusters -= num_md_clusters;
5637 : 1565 : bs->total_data_clusters = bs->num_free_clusters;
5638 : :
5639 : 1565 : cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
5640 : 1565 : cpl.u.bs_handle.cb_fn = cb_fn;
5641 : 1565 : cpl.u.bs_handle.cb_arg = cb_arg;
5642 : 1565 : cpl.u.bs_handle.bs = bs;
5643 : :
5644 : 1565 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5645 [ - + ]: 1565 : if (!seq) {
5646 : 0 : spdk_free(ctx->super);
5647 : 0 : free(ctx);
5648 : 0 : bs_free(bs);
5649 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5650 : 0 : return;
5651 : : }
5652 : :
5653 : 1565 : batch = bs_sequence_to_batch(seq, bs_init_trim_cpl, ctx);
5654 : :
5655 : : /* Clear metadata space */
5656 : 1565 : bs_batch_write_zeroes_dev(batch, 0, num_md_lba);
5657 : :
5658 : 1565 : lba = num_md_lba;
5659 : 1565 : lba_count = ctx->bs->dev->blockcnt - lba;
5660 [ + + + ]: 1565 : switch (opts.clear_method) {
5661 : 1506 : case BS_CLEAR_WITH_UNMAP:
5662 : : /* Trim data clusters */
5663 : 1506 : bs_batch_unmap_dev(batch, lba, lba_count);
5664 : 1506 : break;
5665 : 1 : case BS_CLEAR_WITH_WRITE_ZEROES:
5666 : : /* Write_zeroes to data clusters */
5667 : 1 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
5668 : 1 : break;
5669 : 58 : case BS_CLEAR_WITH_NONE:
5670 : : default:
5671 : 58 : break;
5672 : : }
5673 : :
5674 : 1565 : bs_batch_close(batch);
5675 : : }
5676 : :
5677 : : /* END spdk_bs_init */
5678 : :
5679 : : /* START spdk_bs_destroy */
5680 : :
5681 : : static void
5682 : 108 : bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5683 : : {
5684 : 108 : struct spdk_bs_load_ctx *ctx = cb_arg;
5685 : 108 : struct spdk_blob_store *bs = ctx->bs;
5686 : :
5687 : : /*
5688 : : * We need to defer calling bs_call_cpl() until after
5689 : : * dev destruction, so tuck these away for later use.
5690 : : */
5691 : 108 : bs->unload_err = bserrno;
5692 [ - + - + ]: 108 : memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
5693 : 108 : seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
5694 : :
5695 : 108 : bs_sequence_finish(seq, bserrno);
5696 : :
5697 : 108 : bs_free(bs);
5698 : 108 : free(ctx);
5699 : 108 : }
5700 : :
5701 : : void
5702 : 108 : spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn,
5703 : : void *cb_arg)
5704 : : {
5705 : 87 : struct spdk_bs_cpl cpl;
5706 : : spdk_bs_sequence_t *seq;
5707 : : struct spdk_bs_load_ctx *ctx;
5708 : :
5709 [ - + - + ]: 108 : SPDK_DEBUGLOG(blob, "Destroying blobstore\n");
5710 : :
5711 [ - + ]: 108 : if (!RB_EMPTY(&bs->open_blobs)) {
5712 : 0 : SPDK_ERRLOG("Blobstore still has open blobs\n");
5713 : 0 : cb_fn(cb_arg, -EBUSY);
5714 : 0 : return;
5715 : : }
5716 : :
5717 : 108 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5718 : 108 : cpl.u.bs_basic.cb_fn = cb_fn;
5719 : 108 : cpl.u.bs_basic.cb_arg = cb_arg;
5720 : :
5721 : 108 : ctx = calloc(1, sizeof(*ctx));
5722 [ - + ]: 108 : if (!ctx) {
5723 : 0 : cb_fn(cb_arg, -ENOMEM);
5724 : 0 : return;
5725 : : }
5726 : :
5727 : 108 : ctx->bs = bs;
5728 : :
5729 : 108 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5730 [ - + ]: 108 : if (!seq) {
5731 : 0 : free(ctx);
5732 : 0 : cb_fn(cb_arg, -ENOMEM);
5733 : 0 : return;
5734 : : }
5735 : :
5736 : : /* Write zeroes to the super block */
5737 : 108 : bs_sequence_write_zeroes_dev(seq,
5738 : : bs_page_to_lba(bs, 0),
5739 : : bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)),
5740 : : bs_destroy_trim_cpl, ctx);
5741 : : }
5742 : :
5743 : : /* END spdk_bs_destroy */
5744 : :
5745 : : /* START spdk_bs_unload */
5746 : :
5747 : : static void
5748 : 2098 : bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno)
5749 : : {
5750 : 2098 : spdk_bs_sequence_t *seq = ctx->seq;
5751 : :
5752 : 2098 : spdk_free(ctx->super);
5753 : :
5754 : : /*
5755 : : * We need to defer calling bs_call_cpl() until after
5756 : : * dev destruction, so tuck these away for later use.
5757 : : */
5758 : 2098 : ctx->bs->unload_err = bserrno;
5759 [ - + - + ]: 2098 : memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
5760 : 2098 : seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
5761 : :
5762 : 2098 : bs_sequence_finish(seq, bserrno);
5763 : :
5764 : 2098 : bs_free(ctx->bs);
5765 : 2098 : free(ctx);
5766 : 2098 : }
5767 : :
5768 : : static void
5769 : 2098 : bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5770 : : {
5771 : 2098 : struct spdk_bs_load_ctx *ctx = cb_arg;
5772 : :
5773 : 2098 : bs_unload_finish(ctx, bserrno);
5774 : 2098 : }
5775 : :
5776 : : static void
5777 : 2098 : bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5778 : : {
5779 : 2098 : struct spdk_bs_load_ctx *ctx = cb_arg;
5780 : :
5781 : 2098 : spdk_free(ctx->mask);
5782 : :
5783 [ - + ]: 2098 : if (bserrno != 0) {
5784 : 0 : bs_unload_finish(ctx, bserrno);
5785 : 0 : return;
5786 : : }
5787 : :
5788 : 2098 : ctx->super->clean = 1;
5789 : :
5790 : 2098 : bs_write_super(seq, ctx->bs, ctx->super, bs_unload_write_super_cpl, ctx);
5791 : : }
5792 : :
5793 : : static void
5794 : 2098 : bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5795 : : {
5796 : 2098 : struct spdk_bs_load_ctx *ctx = cb_arg;
5797 : :
5798 : 2098 : spdk_free(ctx->mask);
5799 : 2098 : ctx->mask = NULL;
5800 : :
5801 [ - + ]: 2098 : if (bserrno != 0) {
5802 : 0 : bs_unload_finish(ctx, bserrno);
5803 : 0 : return;
5804 : : }
5805 : :
5806 : 2098 : bs_write_used_clusters(seq, ctx, bs_unload_write_used_clusters_cpl);
5807 : : }
5808 : :
5809 : : static void
5810 : 2098 : bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5811 : : {
5812 : 2098 : struct spdk_bs_load_ctx *ctx = cb_arg;
5813 : :
5814 : 2098 : spdk_free(ctx->mask);
5815 : 2098 : ctx->mask = NULL;
5816 : :
5817 [ - + ]: 2098 : if (bserrno != 0) {
5818 : 0 : bs_unload_finish(ctx, bserrno);
5819 : 0 : return;
5820 : : }
5821 : :
5822 : 2098 : bs_write_used_blobids(seq, ctx, bs_unload_write_used_blobids_cpl);
5823 : : }
5824 : :
5825 : : static void
5826 : 2098 : bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5827 : : {
5828 : 2098 : struct spdk_bs_load_ctx *ctx = cb_arg;
5829 : : int rc;
5830 : :
5831 [ - + ]: 2098 : if (bserrno != 0) {
5832 : 0 : bs_unload_finish(ctx, bserrno);
5833 : 0 : return;
5834 : : }
5835 : :
5836 : 2098 : rc = bs_super_validate(ctx->super, ctx->bs);
5837 [ - + ]: 2098 : if (rc != 0) {
5838 : 0 : bs_unload_finish(ctx, rc);
5839 : 0 : return;
5840 : : }
5841 : :
5842 : 2098 : bs_write_used_md(seq, cb_arg, bs_unload_write_used_pages_cpl);
5843 : : }
5844 : :
5845 : : void
5846 : 2138 : spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
5847 : : {
5848 : 2110 : struct spdk_bs_cpl cpl;
5849 : : struct spdk_bs_load_ctx *ctx;
5850 : :
5851 [ - + - + ]: 2138 : SPDK_DEBUGLOG(blob, "Syncing blobstore\n");
5852 : :
5853 : : /*
5854 : : * If external snapshot channels are being destroyed while the blobstore is unloaded, the
5855 : : * unload is deferred until after the channel destruction completes.
5856 : : */
5857 [ + + ]: 2138 : if (bs->esnap_channels_unloading != 0) {
5858 [ - + ]: 28 : if (bs->esnap_unload_cb_fn != NULL) {
5859 : 0 : SPDK_ERRLOG("Blobstore unload in progress\n");
5860 : 0 : cb_fn(cb_arg, -EBUSY);
5861 : 0 : return;
5862 : : }
5863 [ - + - + ]: 28 : SPDK_DEBUGLOG(blob_esnap, "Blobstore unload deferred: %" PRIu32
5864 : : " esnap clones are unloading\n", bs->esnap_channels_unloading);
5865 : 28 : bs->esnap_unload_cb_fn = cb_fn;
5866 : 28 : bs->esnap_unload_cb_arg = cb_arg;
5867 : 28 : return;
5868 : : }
5869 [ + + ]: 2110 : if (bs->esnap_unload_cb_fn != NULL) {
5870 [ - + - + ]: 28 : SPDK_DEBUGLOG(blob_esnap, "Blobstore deferred unload progressing\n");
5871 [ - + ]: 28 : assert(bs->esnap_unload_cb_fn == cb_fn);
5872 [ - + ]: 28 : assert(bs->esnap_unload_cb_arg == cb_arg);
5873 : 28 : bs->esnap_unload_cb_fn = NULL;
5874 : 28 : bs->esnap_unload_cb_arg = NULL;
5875 : : }
5876 : :
5877 [ + + ]: 2110 : if (!RB_EMPTY(&bs->open_blobs)) {
5878 : 12 : SPDK_ERRLOG("Blobstore still has open blobs\n");
5879 : 12 : cb_fn(cb_arg, -EBUSY);
5880 : 12 : return;
5881 : : }
5882 : :
5883 : 2098 : ctx = calloc(1, sizeof(*ctx));
5884 [ - + ]: 2098 : if (!ctx) {
5885 : 0 : cb_fn(cb_arg, -ENOMEM);
5886 : 0 : return;
5887 : : }
5888 : :
5889 : 2098 : ctx->bs = bs;
5890 : :
5891 : 2098 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
5892 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
5893 [ - + ]: 2098 : if (!ctx->super) {
5894 : 0 : free(ctx);
5895 : 0 : cb_fn(cb_arg, -ENOMEM);
5896 : 0 : return;
5897 : : }
5898 : :
5899 : 2098 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5900 : 2098 : cpl.u.bs_basic.cb_fn = cb_fn;
5901 : 2098 : cpl.u.bs_basic.cb_arg = cb_arg;
5902 : :
5903 : 2098 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5904 [ - + ]: 2098 : if (!ctx->seq) {
5905 : 0 : spdk_free(ctx->super);
5906 : 0 : free(ctx);
5907 : 0 : cb_fn(cb_arg, -ENOMEM);
5908 : 0 : return;
5909 : : }
5910 : :
5911 : : /* Read super block */
5912 : 2098 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
5913 : 2098 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
5914 : : bs_unload_read_super_cpl, ctx);
5915 : : }
5916 : :
5917 : : /* END spdk_bs_unload */
5918 : :
5919 : : /* START spdk_bs_set_super */
5920 : :
5921 : : struct spdk_bs_set_super_ctx {
5922 : : struct spdk_blob_store *bs;
5923 : : struct spdk_bs_super_block *super;
5924 : : };
5925 : :
5926 : : static void
5927 : 170 : bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5928 : : {
5929 : 170 : struct spdk_bs_set_super_ctx *ctx = cb_arg;
5930 : :
5931 [ - + ]: 170 : if (bserrno != 0) {
5932 : 0 : SPDK_ERRLOG("Unable to write to super block of blobstore\n");
5933 : : }
5934 : :
5935 : 170 : spdk_free(ctx->super);
5936 : :
5937 : 170 : bs_sequence_finish(seq, bserrno);
5938 : :
5939 : 170 : free(ctx);
5940 : 170 : }
5941 : :
5942 : : static void
5943 : 170 : bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5944 : : {
5945 : 170 : struct spdk_bs_set_super_ctx *ctx = cb_arg;
5946 : : int rc;
5947 : :
5948 [ - + ]: 170 : if (bserrno != 0) {
5949 : 0 : SPDK_ERRLOG("Unable to read super block of blobstore\n");
5950 : 0 : spdk_free(ctx->super);
5951 : 0 : bs_sequence_finish(seq, bserrno);
5952 : 0 : free(ctx);
5953 : 0 : return;
5954 : : }
5955 : :
5956 : 170 : rc = bs_super_validate(ctx->super, ctx->bs);
5957 [ - + ]: 170 : if (rc != 0) {
5958 : 0 : SPDK_ERRLOG("Not a valid super block\n");
5959 : 0 : spdk_free(ctx->super);
5960 : 0 : bs_sequence_finish(seq, rc);
5961 : 0 : free(ctx);
5962 : 0 : return;
5963 : : }
5964 : :
5965 : 170 : bs_write_super(seq, ctx->bs, ctx->super, bs_set_super_write_cpl, ctx);
5966 : : }
5967 : :
5968 : : void
5969 : 170 : spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
5970 : : spdk_bs_op_complete cb_fn, void *cb_arg)
5971 : : {
5972 : 134 : struct spdk_bs_cpl cpl;
5973 : : spdk_bs_sequence_t *seq;
5974 : : struct spdk_bs_set_super_ctx *ctx;
5975 : :
5976 [ - + - + ]: 170 : SPDK_DEBUGLOG(blob, "Setting super blob id on blobstore\n");
5977 : :
5978 : 170 : ctx = calloc(1, sizeof(*ctx));
5979 [ - + ]: 170 : if (!ctx) {
5980 : 0 : cb_fn(cb_arg, -ENOMEM);
5981 : 0 : return;
5982 : : }
5983 : :
5984 : 170 : ctx->bs = bs;
5985 : :
5986 : 170 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
5987 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
5988 [ - + ]: 170 : if (!ctx->super) {
5989 : 0 : free(ctx);
5990 : 0 : cb_fn(cb_arg, -ENOMEM);
5991 : 0 : return;
5992 : : }
5993 : :
5994 : 170 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5995 : 170 : cpl.u.bs_basic.cb_fn = cb_fn;
5996 : 170 : cpl.u.bs_basic.cb_arg = cb_arg;
5997 : :
5998 : 170 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5999 [ - + ]: 170 : if (!seq) {
6000 : 0 : spdk_free(ctx->super);
6001 : 0 : free(ctx);
6002 : 0 : cb_fn(cb_arg, -ENOMEM);
6003 : 0 : return;
6004 : : }
6005 : :
6006 : 170 : bs->super_blob = blobid;
6007 : :
6008 : : /* Read super block */
6009 : 170 : bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
6010 : 170 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
6011 : : bs_set_super_read_cpl, ctx);
6012 : : }
6013 : :
6014 : : /* END spdk_bs_set_super */
6015 : :
6016 : : void
6017 : 101 : spdk_bs_get_super(struct spdk_blob_store *bs,
6018 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6019 : : {
6020 [ + + ]: 101 : if (bs->super_blob == SPDK_BLOBID_INVALID) {
6021 : 12 : cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
6022 : : } else {
6023 : 89 : cb_fn(cb_arg, bs->super_blob, 0);
6024 : : }
6025 : 101 : }
6026 : :
6027 : : uint64_t
6028 : 1642 : spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
6029 : : {
6030 : 1642 : return bs->cluster_sz;
6031 : : }
6032 : :
6033 : : uint64_t
6034 : 215 : spdk_bs_get_page_size(struct spdk_blob_store *bs)
6035 : : {
6036 : 215 : return SPDK_BS_PAGE_SIZE;
6037 : : }
6038 : :
6039 : : uint64_t
6040 : 1588395 : spdk_bs_get_io_unit_size(struct spdk_blob_store *bs)
6041 : : {
6042 : 1588395 : return bs->io_unit_size;
6043 : : }
6044 : :
6045 : : uint64_t
6046 : 1794 : spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
6047 : : {
6048 : 1794 : return bs->num_free_clusters;
6049 : : }
6050 : :
6051 : : uint64_t
6052 : 447 : spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs)
6053 : : {
6054 : 447 : return bs->total_data_clusters;
6055 : : }
6056 : :
6057 : : static int
6058 : 9001 : bs_register_md_thread(struct spdk_blob_store *bs)
6059 : : {
6060 : 9001 : bs->md_channel = spdk_get_io_channel(bs);
6061 [ - + ]: 9001 : if (!bs->md_channel) {
6062 : 0 : SPDK_ERRLOG("Failed to get IO channel.\n");
6063 : 0 : return -1;
6064 : : }
6065 : :
6066 : 9001 : return 0;
6067 : : }
6068 : :
6069 : : static int
6070 : 9001 : bs_unregister_md_thread(struct spdk_blob_store *bs)
6071 : : {
6072 : 9001 : spdk_put_io_channel(bs->md_channel);
6073 : :
6074 : 9001 : return 0;
6075 : : }
6076 : :
6077 : : spdk_blob_id
6078 : 3692 : spdk_blob_get_id(struct spdk_blob *blob)
6079 : : {
6080 [ - + ]: 3692 : assert(blob != NULL);
6081 : :
6082 : 3692 : return blob->id;
6083 : : }
6084 : :
6085 : : uint64_t
6086 : 105 : spdk_blob_get_num_pages(struct spdk_blob *blob)
6087 : : {
6088 [ - + ]: 105 : assert(blob != NULL);
6089 : :
6090 : 105 : return bs_cluster_to_page(blob->bs, blob->active.num_clusters);
6091 : : }
6092 : :
6093 : : uint64_t
6094 : 104 : spdk_blob_get_num_io_units(struct spdk_blob *blob)
6095 : : {
6096 [ - + ]: 104 : assert(blob != NULL);
6097 : :
6098 : 104 : return spdk_blob_get_num_pages(blob) * bs_io_unit_per_page(blob->bs);
6099 : : }
6100 : :
6101 : : uint64_t
6102 : 285230 : spdk_blob_get_num_clusters(struct spdk_blob *blob)
6103 : : {
6104 [ - + ]: 285230 : assert(blob != NULL);
6105 : :
6106 : 285230 : return blob->active.num_clusters;
6107 : : }
6108 : :
6109 : : uint64_t
6110 : 2174 : spdk_blob_get_num_allocated_clusters(struct spdk_blob *blob)
6111 : : {
6112 [ - + ]: 2174 : assert(blob != NULL);
6113 : :
6114 : 2174 : return blob->active.num_allocated_clusters;
6115 : : }
6116 : :
6117 : : static uint64_t
6118 : 102 : blob_find_io_unit(struct spdk_blob *blob, uint64_t offset, bool is_allocated)
6119 : : {
6120 : 102 : uint64_t blob_io_unit_num = spdk_blob_get_num_io_units(blob);
6121 : :
6122 [ + + ]: 207 : while (offset < blob_io_unit_num) {
6123 [ + + ]: 190 : if (bs_io_unit_is_allocated(blob, offset) == is_allocated) {
6124 : 85 : return offset;
6125 : : }
6126 : :
6127 : 105 : offset += bs_num_io_units_to_cluster_boundary(blob, offset);
6128 : : }
6129 : :
6130 : 17 : return UINT64_MAX;
6131 : : }
6132 : :
6133 : : uint64_t
6134 : 51 : spdk_blob_get_next_allocated_io_unit(struct spdk_blob *blob, uint64_t offset)
6135 : : {
6136 : 51 : return blob_find_io_unit(blob, offset, true);
6137 : : }
6138 : :
6139 : : uint64_t
6140 : 51 : spdk_blob_get_next_unallocated_io_unit(struct spdk_blob *blob, uint64_t offset)
6141 : : {
6142 : 51 : return blob_find_io_unit(blob, offset, false);
6143 : : }
6144 : :
6145 : : /* START spdk_bs_create_blob */
6146 : :
6147 : : static void
6148 : 6803 : bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
6149 : : {
6150 : 6803 : struct spdk_blob *blob = cb_arg;
6151 : 6803 : uint32_t page_idx = bs_blobid_to_page(blob->id);
6152 : :
6153 [ - + ]: 6803 : if (bserrno != 0) {
6154 : 0 : spdk_spin_lock(&blob->bs->used_lock);
6155 : 0 : spdk_bit_array_clear(blob->bs->used_blobids, page_idx);
6156 : 0 : bs_release_md_page(blob->bs, page_idx);
6157 : 0 : spdk_spin_unlock(&blob->bs->used_lock);
6158 : : }
6159 : :
6160 : 6803 : blob_free(blob);
6161 : :
6162 : 6803 : bs_sequence_finish(seq, bserrno);
6163 : 6803 : }
6164 : :
6165 : : static int
6166 : 13670 : blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs,
6167 : : bool internal)
6168 : : {
6169 : : uint64_t i;
6170 : 13670 : size_t value_len = 0;
6171 : : int rc;
6172 : 13670 : const void *value = NULL;
6173 [ + + + + ]: 13670 : if (xattrs->count > 0 && xattrs->get_value == NULL) {
6174 : 24 : return -EINVAL;
6175 : : }
6176 [ + + ]: 15253 : for (i = 0; i < xattrs->count; i++) {
6177 : 1619 : xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len);
6178 [ + + - + ]: 1619 : if (value == NULL || value_len == 0) {
6179 : 12 : return -EINVAL;
6180 : : }
6181 : 1607 : rc = blob_set_xattr(blob, xattrs->names[i], value, value_len, internal);
6182 [ - + ]: 1607 : if (rc < 0) {
6183 : 0 : return rc;
6184 : : }
6185 : : }
6186 : 13634 : return 0;
6187 : : }
6188 : :
6189 : : static void
6190 : 5881 : blob_opts_copy(const struct spdk_blob_opts *src, struct spdk_blob_opts *dst)
6191 : : {
6192 : : #define FIELD_OK(field) \
6193 : : offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size
6194 : :
6195 : : #define SET_FIELD(field) \
6196 : : if (FIELD_OK(field)) { \
6197 : : dst->field = src->field; \
6198 : : } \
6199 : :
6200 [ + - ]: 5881 : SET_FIELD(num_clusters);
6201 [ + - - + ]: 5881 : SET_FIELD(thin_provision);
6202 [ + - ]: 5881 : SET_FIELD(clear_method);
6203 : :
6204 [ + - ]: 5881 : if (FIELD_OK(xattrs)) {
6205 [ - + - + ]: 5881 : memcpy(&dst->xattrs, &src->xattrs, sizeof(src->xattrs));
6206 : : }
6207 : :
6208 [ + - - + ]: 5881 : SET_FIELD(use_extent_table);
6209 [ + - ]: 5881 : SET_FIELD(esnap_id);
6210 [ + - ]: 5881 : SET_FIELD(esnap_id_len);
6211 : :
6212 : 5881 : dst->opts_size = src->opts_size;
6213 : :
6214 : : /* You should not remove this statement, but need to update the assert statement
6215 : : * if you add a new field, and also add a corresponding SET_FIELD statement */
6216 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_opts) == 80, "Incorrect size");
6217 : :
6218 : : #undef FIELD_OK
6219 : : #undef SET_FIELD
6220 : 5881 : }
6221 : :
6222 : : static void
6223 : 6853 : bs_create_blob(struct spdk_blob_store *bs,
6224 : : const struct spdk_blob_opts *opts,
6225 : : const struct spdk_blob_xattr_opts *internal_xattrs,
6226 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6227 : : {
6228 : : struct spdk_blob *blob;
6229 : : uint32_t page_idx;
6230 : 6708 : struct spdk_bs_cpl cpl;
6231 : 6708 : struct spdk_blob_opts opts_local;
6232 : 6708 : struct spdk_blob_xattr_opts internal_xattrs_default;
6233 : : spdk_bs_sequence_t *seq;
6234 : : spdk_blob_id id;
6235 : : int rc;
6236 : :
6237 [ - + ]: 6853 : assert(spdk_get_thread() == bs->md_thread);
6238 : :
6239 : 6853 : spdk_spin_lock(&bs->used_lock);
6240 : 6853 : page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
6241 [ - + ]: 6853 : if (page_idx == UINT32_MAX) {
6242 : 0 : spdk_spin_unlock(&bs->used_lock);
6243 : 0 : cb_fn(cb_arg, 0, -ENOMEM);
6244 : 145 : return;
6245 : : }
6246 : 6853 : spdk_bit_array_set(bs->used_blobids, page_idx);
6247 : 6853 : bs_claim_md_page(bs, page_idx);
6248 : 6853 : spdk_spin_unlock(&bs->used_lock);
6249 : :
6250 : 6853 : id = bs_page_to_blobid(page_idx);
6251 : :
6252 [ - + - + ]: 6853 : SPDK_DEBUGLOG(blob, "Creating blob with id 0x%" PRIx64 " at page %u\n", id, page_idx);
6253 : :
6254 : 6853 : spdk_blob_opts_init(&opts_local, sizeof(opts_local));
6255 [ + + ]: 6853 : if (opts) {
6256 : 5881 : blob_opts_copy(opts, &opts_local);
6257 : : }
6258 : :
6259 : 6853 : blob = blob_alloc(bs, id);
6260 [ - + ]: 6853 : if (!blob) {
6261 : 0 : rc = -ENOMEM;
6262 : 0 : goto error;
6263 : : }
6264 : :
6265 [ - + ]: 6853 : blob->use_extent_table = opts_local.use_extent_table;
6266 [ + + + + ]: 6853 : if (blob->use_extent_table) {
6267 : 4081 : blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE;
6268 : : }
6269 : :
6270 [ + + ]: 6853 : if (!internal_xattrs) {
6271 : 5992 : blob_xattrs_init(&internal_xattrs_default);
6272 : 5992 : internal_xattrs = &internal_xattrs_default;
6273 : : }
6274 : :
6275 : 6853 : rc = blob_set_xattrs(blob, &opts_local.xattrs, false);
6276 [ + + ]: 6853 : if (rc < 0) {
6277 : 36 : goto error;
6278 : : }
6279 : :
6280 : 6817 : rc = blob_set_xattrs(blob, internal_xattrs, true);
6281 [ - + ]: 6817 : if (rc < 0) {
6282 : 0 : goto error;
6283 : : }
6284 : :
6285 [ + + + + ]: 6817 : if (opts_local.thin_provision) {
6286 : 1165 : blob_set_thin_provision(blob);
6287 : : }
6288 : :
6289 : 6817 : blob_set_clear_method(blob, opts_local.clear_method);
6290 : :
6291 [ + + ]: 6817 : if (opts_local.esnap_id != NULL) {
6292 [ - + ]: 190 : if (opts_local.esnap_id_len > UINT16_MAX) {
6293 : 0 : SPDK_ERRLOG("esnap id length %" PRIu64 "is too long\n",
6294 : : opts_local.esnap_id_len);
6295 : 0 : rc = -EINVAL;
6296 : 0 : goto error;
6297 : :
6298 : : }
6299 : 190 : blob_set_thin_provision(blob);
6300 : 190 : blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
6301 : 190 : rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID,
6302 : 190 : opts_local.esnap_id, opts_local.esnap_id_len, true);
6303 [ - + ]: 190 : if (rc != 0) {
6304 : 0 : goto error;
6305 : : }
6306 : : }
6307 : :
6308 : 6817 : rc = blob_resize(blob, opts_local.num_clusters);
6309 [ + + ]: 6817 : if (rc < 0) {
6310 : 14 : goto error;
6311 : : }
6312 : 6803 : cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
6313 : 6803 : cpl.u.blobid.cb_fn = cb_fn;
6314 : 6803 : cpl.u.blobid.cb_arg = cb_arg;
6315 : 6803 : cpl.u.blobid.blobid = blob->id;
6316 : :
6317 : 6803 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
6318 [ - + ]: 6803 : if (!seq) {
6319 : 0 : rc = -ENOMEM;
6320 : 0 : goto error;
6321 : : }
6322 : :
6323 : 6803 : blob_persist(seq, blob, bs_create_blob_cpl, blob);
6324 : 6803 : return;
6325 : :
6326 : 50 : error:
6327 : 50 : SPDK_ERRLOG("Failed to create blob: %s, size in clusters/size: %lu (clusters)\n",
6328 : : spdk_strerror(rc), opts_local.num_clusters);
6329 [ + - ]: 50 : if (blob != NULL) {
6330 : 50 : blob_free(blob);
6331 : : }
6332 : 50 : spdk_spin_lock(&bs->used_lock);
6333 : 50 : spdk_bit_array_clear(bs->used_blobids, page_idx);
6334 : 50 : bs_release_md_page(bs, page_idx);
6335 : 50 : spdk_spin_unlock(&bs->used_lock);
6336 : 50 : cb_fn(cb_arg, 0, rc);
6337 : : }
6338 : :
6339 : : void
6340 : 924 : spdk_bs_create_blob(struct spdk_blob_store *bs,
6341 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6342 : : {
6343 : 924 : bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg);
6344 : 924 : }
6345 : :
6346 : : void
6347 : 5044 : spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts,
6348 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6349 : : {
6350 : 5044 : bs_create_blob(bs, opts, NULL, cb_fn, cb_arg);
6351 : 5044 : }
6352 : :
6353 : : /* END spdk_bs_create_blob */
6354 : :
6355 : : /* START blob_cleanup */
6356 : :
6357 : : struct spdk_clone_snapshot_ctx {
6358 : : struct spdk_bs_cpl cpl;
6359 : : int bserrno;
6360 : : bool frozen;
6361 : :
6362 : : struct spdk_io_channel *channel;
6363 : :
6364 : : /* Current cluster for inflate operation */
6365 : : uint64_t cluster;
6366 : :
6367 : : /* For inflation force allocation of all unallocated clusters and remove
6368 : : * thin-provisioning. Otherwise only decouple parent and keep clone thin. */
6369 : : bool allocate_all;
6370 : :
6371 : : struct {
6372 : : spdk_blob_id id;
6373 : : struct spdk_blob *blob;
6374 : : bool md_ro;
6375 : : } original;
6376 : : struct {
6377 : : spdk_blob_id id;
6378 : : struct spdk_blob *blob;
6379 : : } new;
6380 : :
6381 : : /* xattrs specified for snapshot/clones only. They have no impact on
6382 : : * the original blobs xattrs. */
6383 : : const struct spdk_blob_xattr_opts *xattrs;
6384 : : };
6385 : :
6386 : : static void
6387 : 1082 : bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno)
6388 : : {
6389 : 1082 : struct spdk_clone_snapshot_ctx *ctx = cb_arg;
6390 : 1082 : struct spdk_bs_cpl *cpl = &ctx->cpl;
6391 : :
6392 [ + + ]: 1082 : if (bserrno != 0) {
6393 [ - + ]: 18 : if (ctx->bserrno != 0) {
6394 : 0 : SPDK_ERRLOG("Cleanup error %d\n", bserrno);
6395 : : } else {
6396 : 18 : ctx->bserrno = bserrno;
6397 : : }
6398 : : }
6399 : :
6400 [ + + - ]: 1082 : switch (cpl->type) {
6401 : 894 : case SPDK_BS_CPL_TYPE_BLOBID:
6402 : 894 : cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno);
6403 : 894 : break;
6404 : 188 : case SPDK_BS_CPL_TYPE_BLOB_BASIC:
6405 : 188 : cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
6406 : 188 : break;
6407 : 0 : default:
6408 : 0 : SPDK_UNREACHABLE();
6409 : : break;
6410 : : }
6411 : :
6412 : 1082 : free(ctx);
6413 : 1082 : }
6414 : :
6415 : : static void
6416 : 1037 : bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
6417 : : {
6418 : 1037 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6419 : 1037 : struct spdk_blob *origblob = ctx->original.blob;
6420 : :
6421 [ - + ]: 1037 : if (bserrno != 0) {
6422 [ # # ]: 0 : if (ctx->bserrno != 0) {
6423 : 0 : SPDK_ERRLOG("Unfreeze error %d\n", bserrno);
6424 : : } else {
6425 : 0 : ctx->bserrno = bserrno;
6426 : : }
6427 : : }
6428 : :
6429 : 1037 : ctx->original.id = origblob->id;
6430 : 1037 : origblob->locked_operation_in_progress = false;
6431 : :
6432 : : /* Revert md_ro to original state */
6433 [ - + ]: 1037 : origblob->md_ro = ctx->original.md_ro;
6434 : :
6435 : 1037 : spdk_blob_close(origblob, bs_clone_snapshot_cleanup_finish, ctx);
6436 : 1037 : }
6437 : :
6438 : : static void
6439 : 1037 : bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno)
6440 : : {
6441 : 1037 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6442 : 1037 : struct spdk_blob *origblob = ctx->original.blob;
6443 : :
6444 [ + + ]: 1037 : if (bserrno != 0) {
6445 [ + + ]: 73 : if (ctx->bserrno != 0) {
6446 : 12 : SPDK_ERRLOG("Cleanup error %d\n", bserrno);
6447 : : } else {
6448 : 61 : ctx->bserrno = bserrno;
6449 : : }
6450 : : }
6451 : :
6452 [ + + + + ]: 1037 : if (ctx->frozen) {
6453 : : /* Unfreeze any outstanding I/O */
6454 : 660 : blob_unfreeze_io(origblob, bs_snapshot_unfreeze_cpl, ctx);
6455 : : } else {
6456 : 377 : bs_snapshot_unfreeze_cpl(ctx, 0);
6457 : : }
6458 : :
6459 : 1037 : }
6460 : :
6461 : : static void
6462 : 12 : bs_clone_snapshot_newblob_cleanup(struct spdk_clone_snapshot_ctx *ctx, int bserrno)
6463 : : {
6464 : 12 : struct spdk_blob *newblob = ctx->new.blob;
6465 : :
6466 [ + - ]: 12 : if (bserrno != 0) {
6467 [ - + ]: 12 : if (ctx->bserrno != 0) {
6468 : 0 : SPDK_ERRLOG("Cleanup error %d\n", bserrno);
6469 : : } else {
6470 : 12 : ctx->bserrno = bserrno;
6471 : : }
6472 : : }
6473 : :
6474 : 12 : ctx->new.id = newblob->id;
6475 : 12 : spdk_blob_close(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
6476 : 12 : }
6477 : :
6478 : : /* END blob_cleanup */
6479 : :
6480 : : /* START spdk_bs_create_snapshot */
6481 : :
6482 : : static void
6483 : 684 : bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2)
6484 : : {
6485 : : uint64_t *cluster_temp;
6486 : : uint64_t num_allocated_clusters_temp;
6487 : : uint32_t *extent_page_temp;
6488 : :
6489 : 684 : cluster_temp = blob1->active.clusters;
6490 : 684 : blob1->active.clusters = blob2->active.clusters;
6491 : 684 : blob2->active.clusters = cluster_temp;
6492 : :
6493 : 684 : num_allocated_clusters_temp = blob1->active.num_allocated_clusters;
6494 : 684 : blob1->active.num_allocated_clusters = blob2->active.num_allocated_clusters;
6495 : 684 : blob2->active.num_allocated_clusters = num_allocated_clusters_temp;
6496 : :
6497 : 684 : extent_page_temp = blob1->active.extent_pages;
6498 : 684 : blob1->active.extent_pages = blob2->active.extent_pages;
6499 : 684 : blob2->active.extent_pages = extent_page_temp;
6500 : 684 : }
6501 : :
6502 : : /* Copies an internal xattr */
6503 : : static int
6504 : 68 : bs_snapshot_copy_xattr(struct spdk_blob *toblob, struct spdk_blob *fromblob, const char *name)
6505 : : {
6506 : 68 : const void *val = NULL;
6507 : 68 : size_t len;
6508 : : int bserrno;
6509 : :
6510 : 68 : bserrno = blob_get_xattr_value(fromblob, name, &val, &len, true);
6511 [ - + ]: 68 : if (bserrno != 0) {
6512 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " missing %s XATTR\n", fromblob->id, name);
6513 : 0 : return bserrno;
6514 : : }
6515 : :
6516 : 68 : bserrno = blob_set_xattr(toblob, name, val, len, true);
6517 [ - + ]: 68 : if (bserrno != 0) {
6518 : 0 : SPDK_ERRLOG("could not set %s XATTR on blob 0x%" PRIx64 "\n",
6519 : : name, toblob->id);
6520 : 0 : return bserrno;
6521 : : }
6522 : 68 : return 0;
6523 : : }
6524 : :
6525 : : static void
6526 : 648 : bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno)
6527 : : {
6528 : 648 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6529 : 648 : struct spdk_blob *origblob = ctx->original.blob;
6530 : 648 : struct spdk_blob *newblob = ctx->new.blob;
6531 : :
6532 [ + + ]: 648 : if (bserrno != 0) {
6533 : 12 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6534 [ - + ]: 12 : if (blob_is_esnap_clone(newblob)) {
6535 : 0 : bs_snapshot_copy_xattr(origblob, newblob, BLOB_EXTERNAL_SNAPSHOT_ID);
6536 : 0 : origblob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
6537 : : }
6538 : 12 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6539 : 12 : return;
6540 : : }
6541 : :
6542 : : /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */
6543 : 636 : bserrno = blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true);
6544 [ - + ]: 636 : if (bserrno != 0) {
6545 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6546 : 0 : return;
6547 : : }
6548 : :
6549 : 636 : bs_blob_list_add(ctx->original.blob);
6550 : :
6551 : 636 : spdk_blob_set_read_only(newblob);
6552 : :
6553 : : /* sync snapshot metadata */
6554 : 636 : spdk_blob_sync_md(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
6555 : : }
6556 : :
6557 : : static void
6558 : 660 : bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno)
6559 : : {
6560 : 660 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6561 : 660 : struct spdk_blob *origblob = ctx->original.blob;
6562 : 660 : struct spdk_blob *newblob = ctx->new.blob;
6563 : :
6564 [ + + ]: 660 : if (bserrno != 0) {
6565 : : /* return cluster map back to original */
6566 : 12 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6567 : :
6568 : : /* Newblob md sync failed. Valid clusters are only present in origblob.
6569 : : * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occurred.
6570 : : * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */
6571 : 12 : blob_set_thin_provision(newblob);
6572 [ - + ]: 12 : assert(spdk_mem_all_zero(newblob->active.clusters,
6573 : : newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
6574 [ - + ]: 12 : assert(spdk_mem_all_zero(newblob->active.extent_pages,
6575 : : newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
6576 : :
6577 : 12 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6578 : 12 : return;
6579 : : }
6580 : :
6581 : : /* Set internal xattr for snapshot id */
6582 : 648 : bserrno = blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true);
6583 [ - + ]: 648 : if (bserrno != 0) {
6584 : : /* return cluster map back to original */
6585 : 0 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6586 : 0 : blob_set_thin_provision(newblob);
6587 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6588 : 0 : return;
6589 : : }
6590 : :
6591 : : /* Create new back_bs_dev for snapshot */
6592 : 648 : origblob->back_bs_dev = bs_create_blob_bs_dev(newblob);
6593 [ - + ]: 648 : if (origblob->back_bs_dev == NULL) {
6594 : : /* return cluster map back to original */
6595 : 0 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6596 : 0 : blob_set_thin_provision(newblob);
6597 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL);
6598 : 0 : return;
6599 : : }
6600 : :
6601 : : /* Remove the xattr that references an external snapshot */
6602 [ + + ]: 648 : if (blob_is_esnap_clone(origblob)) {
6603 : 40 : origblob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
6604 : 40 : bserrno = blob_remove_xattr(origblob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
6605 [ - + ]: 40 : if (bserrno != 0) {
6606 [ # # ]: 0 : if (bserrno == -ENOENT) {
6607 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " has no " BLOB_EXTERNAL_SNAPSHOT_ID
6608 : : " xattr to remove\n", origblob->id);
6609 : 0 : assert(false);
6610 : : } else {
6611 : : /* return cluster map back to original */
6612 : 0 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6613 : 0 : blob_set_thin_provision(newblob);
6614 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6615 : 0 : return;
6616 : : }
6617 : : }
6618 : : }
6619 : :
6620 : 648 : bs_blob_list_remove(origblob);
6621 : 648 : origblob->parent_id = newblob->id;
6622 : : /* set clone blob as thin provisioned */
6623 : 648 : blob_set_thin_provision(origblob);
6624 : :
6625 : 648 : bs_blob_list_add(newblob);
6626 : :
6627 : : /* sync clone metadata */
6628 : 648 : spdk_blob_sync_md(origblob, bs_snapshot_origblob_sync_cpl, ctx);
6629 : : }
6630 : :
6631 : : static void
6632 : 660 : bs_snapshot_freeze_cpl(void *cb_arg, int rc)
6633 : : {
6634 : 660 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6635 : 660 : struct spdk_blob *origblob = ctx->original.blob;
6636 : 660 : struct spdk_blob *newblob = ctx->new.blob;
6637 : : int bserrno;
6638 : :
6639 [ - + ]: 660 : if (rc != 0) {
6640 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, rc);
6641 : 0 : return;
6642 : : }
6643 : :
6644 : 660 : ctx->frozen = true;
6645 : :
6646 [ + + ]: 660 : if (blob_is_esnap_clone(origblob)) {
6647 : : /* Clean up any channels associated with the original blob id because future IO will
6648 : : * perform IO using the snapshot blob_id.
6649 : : */
6650 : 40 : blob_esnap_destroy_bs_dev_channels(origblob, false, NULL, NULL);
6651 : : }
6652 [ + - ]: 660 : if (newblob->back_bs_dev) {
6653 : 660 : blob_back_bs_destroy(newblob);
6654 : : }
6655 : : /* set new back_bs_dev for snapshot */
6656 : 660 : newblob->back_bs_dev = origblob->back_bs_dev;
6657 : : /* Set invalid flags from origblob */
6658 : 660 : newblob->invalid_flags = origblob->invalid_flags;
6659 : :
6660 : : /* inherit parent from original blob if set */
6661 : 660 : newblob->parent_id = origblob->parent_id;
6662 [ + + + ]: 660 : switch (origblob->parent_id) {
6663 : 40 : case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
6664 : 40 : bserrno = bs_snapshot_copy_xattr(newblob, origblob, BLOB_EXTERNAL_SNAPSHOT_ID);
6665 [ - + ]: 40 : if (bserrno != 0) {
6666 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6667 : 0 : return;
6668 : : }
6669 : 40 : break;
6670 : 459 : case SPDK_BLOBID_INVALID:
6671 : 459 : break;
6672 : 161 : default:
6673 : : /* Set internal xattr for snapshot id */
6674 : 161 : bserrno = blob_set_xattr(newblob, BLOB_SNAPSHOT,
6675 : 161 : &origblob->parent_id, sizeof(spdk_blob_id), true);
6676 [ - + ]: 161 : if (bserrno != 0) {
6677 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6678 : 0 : return;
6679 : : }
6680 : : }
6681 : :
6682 : : /* swap cluster maps */
6683 : 660 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6684 : :
6685 : : /* Set the clear method on the new blob to match the original. */
6686 : 660 : blob_set_clear_method(newblob, origblob->clear_method);
6687 : :
6688 : : /* sync snapshot metadata */
6689 : 660 : spdk_blob_sync_md(newblob, bs_snapshot_newblob_sync_cpl, ctx);
6690 : : }
6691 : :
6692 : : static void
6693 : 672 : bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6694 : : {
6695 : 672 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6696 : 672 : struct spdk_blob *origblob = ctx->original.blob;
6697 : 672 : struct spdk_blob *newblob = _blob;
6698 : :
6699 [ + + ]: 672 : if (bserrno != 0) {
6700 : 12 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6701 : 12 : return;
6702 : : }
6703 : :
6704 : 660 : ctx->new.blob = newblob;
6705 [ - + ]: 660 : assert(spdk_blob_is_thin_provisioned(newblob));
6706 [ - + ]: 660 : assert(spdk_mem_all_zero(newblob->active.clusters,
6707 : : newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
6708 [ - + ]: 660 : assert(spdk_mem_all_zero(newblob->active.extent_pages,
6709 : : newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
6710 : :
6711 : 660 : blob_freeze_io(origblob, bs_snapshot_freeze_cpl, ctx);
6712 : : }
6713 : :
6714 : : static void
6715 : 684 : bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
6716 : : {
6717 : 684 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6718 : 684 : struct spdk_blob *origblob = ctx->original.blob;
6719 : :
6720 [ + + ]: 684 : if (bserrno != 0) {
6721 : 12 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6722 : 12 : return;
6723 : : }
6724 : :
6725 : 672 : ctx->new.id = blobid;
6726 : 672 : ctx->cpl.u.blobid.blobid = blobid;
6727 : :
6728 : 672 : spdk_bs_open_blob(origblob->bs, ctx->new.id, bs_snapshot_newblob_open_cpl, ctx);
6729 : : }
6730 : :
6731 : :
6732 : : static void
6733 : 684 : bs_xattr_snapshot(void *arg, const char *name,
6734 : : const void **value, size_t *value_len)
6735 : : {
6736 [ - + - + ]: 684 : assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0);
6737 : :
6738 : 684 : struct spdk_blob *blob = (struct spdk_blob *)arg;
6739 : 684 : *value = &blob->id;
6740 : 684 : *value_len = sizeof(blob->id);
6741 : 684 : }
6742 : :
6743 : : static void
6744 : 715 : bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6745 : : {
6746 : 715 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6747 : 710 : struct spdk_blob_opts opts;
6748 : 710 : struct spdk_blob_xattr_opts internal_xattrs;
6749 : 715 : char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS };
6750 : :
6751 [ + + ]: 715 : if (bserrno != 0) {
6752 : 18 : bs_clone_snapshot_cleanup_finish(ctx, bserrno);
6753 : 18 : return;
6754 : : }
6755 : :
6756 : 697 : ctx->original.blob = _blob;
6757 : :
6758 [ + + + + : 697 : if (_blob->data_ro || _blob->md_ro) {
- + - + ]
6759 [ - + - + ]: 13 : SPDK_DEBUGLOG(blob, "Cannot create snapshot from read only blob with id 0x%"
6760 : : PRIx64 "\n", _blob->id);
6761 : 13 : ctx->bserrno = -EINVAL;
6762 : 13 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6763 : 13 : return;
6764 : : }
6765 : :
6766 [ - + - + ]: 684 : if (_blob->locked_operation_in_progress) {
6767 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot create snapshot - another operation in progress\n");
6768 : 0 : ctx->bserrno = -EBUSY;
6769 : 0 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6770 : 0 : return;
6771 : : }
6772 : :
6773 : 684 : _blob->locked_operation_in_progress = true;
6774 : :
6775 : 684 : spdk_blob_opts_init(&opts, sizeof(opts));
6776 : 684 : blob_xattrs_init(&internal_xattrs);
6777 : :
6778 : : /* Change the size of new blob to the same as in original blob,
6779 : : * but do not allocate clusters */
6780 : 684 : opts.thin_provision = true;
6781 : 684 : opts.num_clusters = spdk_blob_get_num_clusters(_blob);
6782 [ - + ]: 684 : opts.use_extent_table = _blob->use_extent_table;
6783 : :
6784 : : /* If there are any xattrs specified for snapshot, set them now */
6785 [ + + ]: 684 : if (ctx->xattrs) {
6786 [ - + - + ]: 48 : memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
6787 : : }
6788 : : /* Set internal xattr SNAPSHOT_IN_PROGRESS */
6789 : 684 : internal_xattrs.count = 1;
6790 : 684 : internal_xattrs.ctx = _blob;
6791 : 684 : internal_xattrs.names = xattrs_names;
6792 : 684 : internal_xattrs.get_value = bs_xattr_snapshot;
6793 : :
6794 : 684 : bs_create_blob(_blob->bs, &opts, &internal_xattrs,
6795 : : bs_snapshot_newblob_create_cpl, ctx);
6796 : : }
6797 : :
6798 : : void
6799 : 715 : spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid,
6800 : : const struct spdk_blob_xattr_opts *snapshot_xattrs,
6801 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6802 : : {
6803 : 715 : struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
6804 : :
6805 [ - + ]: 715 : if (!ctx) {
6806 : 0 : cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
6807 : 0 : return;
6808 : : }
6809 : 715 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
6810 : 715 : ctx->cpl.u.blobid.cb_fn = cb_fn;
6811 : 715 : ctx->cpl.u.blobid.cb_arg = cb_arg;
6812 : 715 : ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
6813 : 715 : ctx->bserrno = 0;
6814 : 715 : ctx->frozen = false;
6815 : 715 : ctx->original.id = blobid;
6816 : 715 : ctx->xattrs = snapshot_xattrs;
6817 : :
6818 : 715 : spdk_bs_open_blob(bs, ctx->original.id, bs_snapshot_origblob_open_cpl, ctx);
6819 : : }
6820 : : /* END spdk_bs_create_snapshot */
6821 : :
6822 : : /* START spdk_bs_create_clone */
6823 : :
6824 : : static void
6825 : 165 : bs_xattr_clone(void *arg, const char *name,
6826 : : const void **value, size_t *value_len)
6827 : : {
6828 [ - + - + ]: 165 : assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0);
6829 : :
6830 : 165 : struct spdk_blob *blob = (struct spdk_blob *)arg;
6831 : 165 : *value = &blob->id;
6832 : 165 : *value_len = sizeof(blob->id);
6833 : 165 : }
6834 : :
6835 : : static void
6836 : 165 : bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6837 : : {
6838 : 165 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6839 : 165 : struct spdk_blob *clone = _blob;
6840 : :
6841 : 165 : ctx->new.blob = clone;
6842 : 165 : bs_blob_list_add(clone);
6843 : :
6844 : 165 : spdk_blob_close(clone, bs_clone_snapshot_origblob_cleanup, ctx);
6845 : 165 : }
6846 : :
6847 : : static void
6848 : 165 : bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
6849 : : {
6850 : 165 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6851 : :
6852 : 165 : ctx->cpl.u.blobid.blobid = blobid;
6853 : 165 : spdk_bs_open_blob(ctx->original.blob->bs, blobid, bs_clone_newblob_open_cpl, ctx);
6854 : 165 : }
6855 : :
6856 : : static void
6857 : 179 : bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6858 : : {
6859 : 179 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6860 : 174 : struct spdk_blob_opts opts;
6861 : 174 : struct spdk_blob_xattr_opts internal_xattrs;
6862 : 179 : char *xattr_names[] = { BLOB_SNAPSHOT };
6863 : :
6864 [ - + ]: 179 : if (bserrno != 0) {
6865 : 0 : bs_clone_snapshot_cleanup_finish(ctx, bserrno);
6866 : 0 : return;
6867 : : }
6868 : :
6869 : 179 : ctx->original.blob = _blob;
6870 [ - + ]: 179 : ctx->original.md_ro = _blob->md_ro;
6871 : :
6872 [ + + + + : 179 : if (!_blob->data_ro || !_blob->md_ro) {
- + - + ]
6873 [ - + - + ]: 14 : SPDK_DEBUGLOG(blob, "Clone not from read-only blob\n");
6874 : 14 : ctx->bserrno = -EINVAL;
6875 : 14 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6876 : 14 : return;
6877 : : }
6878 : :
6879 [ - + - + ]: 165 : if (_blob->locked_operation_in_progress) {
6880 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot create clone - another operation in progress\n");
6881 : 0 : ctx->bserrno = -EBUSY;
6882 : 0 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6883 : 0 : return;
6884 : : }
6885 : :
6886 : 165 : _blob->locked_operation_in_progress = true;
6887 : :
6888 : 165 : spdk_blob_opts_init(&opts, sizeof(opts));
6889 : 165 : blob_xattrs_init(&internal_xattrs);
6890 : :
6891 : 165 : opts.thin_provision = true;
6892 : 165 : opts.num_clusters = spdk_blob_get_num_clusters(_blob);
6893 [ - + ]: 165 : opts.use_extent_table = _blob->use_extent_table;
6894 [ + + ]: 165 : if (ctx->xattrs) {
6895 [ - + - + ]: 33 : memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
6896 : : }
6897 : :
6898 : : /* Set internal xattr BLOB_SNAPSHOT */
6899 : 165 : internal_xattrs.count = 1;
6900 : 165 : internal_xattrs.ctx = _blob;
6901 : 165 : internal_xattrs.names = xattr_names;
6902 : 165 : internal_xattrs.get_value = bs_xattr_clone;
6903 : :
6904 : 165 : bs_create_blob(_blob->bs, &opts, &internal_xattrs,
6905 : : bs_clone_newblob_create_cpl, ctx);
6906 : : }
6907 : :
6908 : : void
6909 : 179 : spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid,
6910 : : const struct spdk_blob_xattr_opts *clone_xattrs,
6911 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6912 : : {
6913 : 179 : struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
6914 : :
6915 [ - + ]: 179 : if (!ctx) {
6916 : 0 : cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
6917 : 0 : return;
6918 : : }
6919 : :
6920 : 179 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
6921 : 179 : ctx->cpl.u.blobid.cb_fn = cb_fn;
6922 : 179 : ctx->cpl.u.blobid.cb_arg = cb_arg;
6923 : 179 : ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
6924 : 179 : ctx->bserrno = 0;
6925 : 179 : ctx->xattrs = clone_xattrs;
6926 : 179 : ctx->original.id = blobid;
6927 : :
6928 : 179 : spdk_bs_open_blob(bs, ctx->original.id, bs_clone_origblob_open_cpl, ctx);
6929 : : }
6930 : :
6931 : : /* END spdk_bs_create_clone */
6932 : :
6933 : : /* START spdk_bs_inflate_blob */
6934 : :
6935 : : static void
6936 : 37 : bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno)
6937 : : {
6938 : 37 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6939 : 37 : struct spdk_blob *_blob = ctx->original.blob;
6940 : :
6941 [ - + ]: 37 : if (bserrno != 0) {
6942 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6943 : 0 : return;
6944 : : }
6945 : :
6946 : : /* Temporarily override md_ro flag for MD modification */
6947 : 37 : _blob->md_ro = false;
6948 : :
6949 : 37 : bserrno = blob_set_xattr(_blob, BLOB_SNAPSHOT, &_parent->id, sizeof(spdk_blob_id), true);
6950 [ - + ]: 37 : if (bserrno != 0) {
6951 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6952 : 0 : return;
6953 : : }
6954 : :
6955 [ - + ]: 37 : assert(_parent != NULL);
6956 : :
6957 : 37 : bs_blob_list_remove(_blob);
6958 : 37 : _blob->parent_id = _parent->id;
6959 : :
6960 : 37 : blob_back_bs_destroy(_blob);
6961 : 37 : _blob->back_bs_dev = bs_create_blob_bs_dev(_parent);
6962 : 37 : bs_blob_list_add(_blob);
6963 : :
6964 : 37 : spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
6965 : : }
6966 : :
6967 : : static void
6968 : 175 : bs_inflate_blob_done(struct spdk_clone_snapshot_ctx *ctx)
6969 : : {
6970 : 175 : struct spdk_blob *_blob = ctx->original.blob;
6971 : : struct spdk_blob *_parent;
6972 : :
6973 [ + + + + ]: 175 : if (ctx->allocate_all) {
6974 : : /* remove thin provisioning */
6975 : 101 : bs_blob_list_remove(_blob);
6976 [ + + ]: 101 : if (_blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
6977 : 24 : blob_remove_xattr(_blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
6978 : 24 : _blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
6979 : : } else {
6980 : 77 : blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
6981 : : }
6982 : 101 : _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV;
6983 : 101 : blob_back_bs_destroy(_blob);
6984 : 101 : _blob->parent_id = SPDK_BLOBID_INVALID;
6985 : : } else {
6986 : : /* For now, esnap clones always have allocate_all set. */
6987 [ - + ]: 74 : assert(!blob_is_esnap_clone(_blob));
6988 : :
6989 : 74 : _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob;
6990 [ + + ]: 74 : if (_parent->parent_id != SPDK_BLOBID_INVALID) {
6991 : : /* We must change the parent of the inflated blob */
6992 : 37 : spdk_bs_open_blob(_blob->bs, _parent->parent_id,
6993 : : bs_inflate_blob_set_parent_cpl, ctx);
6994 : 37 : return;
6995 : : }
6996 : :
6997 : 37 : bs_blob_list_remove(_blob);
6998 : 37 : _blob->parent_id = SPDK_BLOBID_INVALID;
6999 : 37 : blob_back_bs_destroy(_blob);
7000 : 37 : _blob->back_bs_dev = bs_create_zeroes_dev();
7001 : : }
7002 : :
7003 : : /* Temporarily override md_ro flag for MD modification */
7004 : 138 : _blob->md_ro = false;
7005 : 138 : blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
7006 : 138 : _blob->state = SPDK_BLOB_STATE_DIRTY;
7007 : :
7008 : 138 : spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
7009 : : }
7010 : :
7011 : : /* Check if cluster needs allocation */
7012 : : static inline bool
7013 : 3674 : bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all)
7014 : : {
7015 : : struct spdk_blob_bs_dev *b;
7016 : :
7017 [ - + ]: 3674 : assert(blob != NULL);
7018 : :
7019 [ + + ]: 3674 : if (blob->active.clusters[cluster] != 0) {
7020 : : /* Cluster is already allocated */
7021 : 106 : return false;
7022 : : }
7023 : :
7024 [ + + ]: 3568 : if (blob->parent_id == SPDK_BLOBID_INVALID) {
7025 : : /* Blob have no parent blob */
7026 : 240 : return allocate_all;
7027 : : }
7028 : :
7029 [ + + ]: 3328 : if (blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
7030 : 192 : return true;
7031 : : }
7032 : :
7033 : 3136 : b = (struct spdk_blob_bs_dev *)blob->back_bs_dev;
7034 [ + + + + ]: 3136 : return (allocate_all || b->blob->active.clusters[cluster] != 0);
7035 : : }
7036 : :
7037 : : static void
7038 : 1559 : bs_inflate_blob_touch_next(void *cb_arg, int bserrno)
7039 : : {
7040 : 1559 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
7041 : 1559 : struct spdk_blob *_blob = ctx->original.blob;
7042 : 1535 : struct spdk_bs_cpl cpl;
7043 : : spdk_bs_user_op_t *op;
7044 : : uint64_t offset;
7045 : :
7046 [ - + ]: 1559 : if (bserrno != 0) {
7047 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
7048 : 0 : return;
7049 : : }
7050 : :
7051 [ + + ]: 2012 : for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) {
7052 [ + + + + ]: 1837 : if (bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) {
7053 : 1384 : break;
7054 : : }
7055 : : }
7056 : :
7057 [ + + ]: 1559 : if (ctx->cluster < _blob->active.num_clusters) {
7058 : 1384 : offset = bs_cluster_to_lba(_blob->bs, ctx->cluster);
7059 : :
7060 : : /* We may safely increment a cluster before copying */
7061 : 1384 : ctx->cluster++;
7062 : :
7063 : : /* Use a dummy 0B read as a context for cluster copy */
7064 : 1384 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
7065 : 1384 : cpl.u.blob_basic.cb_fn = bs_inflate_blob_touch_next;
7066 : 1384 : cpl.u.blob_basic.cb_arg = ctx;
7067 : :
7068 : 1384 : op = bs_user_op_alloc(ctx->channel, &cpl, SPDK_BLOB_READ, _blob,
7069 : : NULL, 0, offset, 0);
7070 [ - + ]: 1384 : if (!op) {
7071 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, -ENOMEM);
7072 : 0 : return;
7073 : : }
7074 : :
7075 : 1384 : bs_allocate_and_copy_cluster(_blob, ctx->channel, offset, op);
7076 : : } else {
7077 : 175 : bs_inflate_blob_done(ctx);
7078 : : }
7079 : : }
7080 : :
7081 : : static void
7082 : 188 : bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
7083 : : {
7084 : 188 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
7085 : : uint64_t clusters_needed;
7086 : : uint64_t i;
7087 : :
7088 [ - + ]: 188 : if (bserrno != 0) {
7089 : 0 : bs_clone_snapshot_cleanup_finish(ctx, bserrno);
7090 : 0 : return;
7091 : : }
7092 : :
7093 : 188 : ctx->original.blob = _blob;
7094 [ - + ]: 188 : ctx->original.md_ro = _blob->md_ro;
7095 : :
7096 [ - + - + ]: 188 : if (_blob->locked_operation_in_progress) {
7097 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot inflate blob - another operation in progress\n");
7098 : 0 : ctx->bserrno = -EBUSY;
7099 : 0 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
7100 : 0 : return;
7101 : : }
7102 : :
7103 : 188 : _blob->locked_operation_in_progress = true;
7104 : :
7105 [ + + + ]: 188 : switch (_blob->parent_id) {
7106 : 25 : case SPDK_BLOBID_INVALID:
7107 [ + + + + ]: 25 : if (!ctx->allocate_all) {
7108 : : /* This blob has no parent, so we cannot decouple it. */
7109 : 13 : SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n");
7110 : 13 : bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL);
7111 : 13 : return;
7112 : : }
7113 : 12 : break;
7114 : 24 : case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
7115 : : /*
7116 : : * It would be better to rely on back_bs_dev->is_zeroes(), to determine which
7117 : : * clusters require allocation. Until there is a blobstore consumer that
7118 : : * uses esnaps with an spdk_bs_dev that implements a useful is_zeroes() it is not
7119 : : * worth the effort.
7120 : : */
7121 : 24 : ctx->allocate_all = true;
7122 : 24 : break;
7123 : 139 : default:
7124 : 139 : break;
7125 : : }
7126 : :
7127 [ - + ]: 175 : if (spdk_blob_is_thin_provisioned(_blob) == false) {
7128 : : /* This is not thin provisioned blob. No need to inflate. */
7129 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, 0);
7130 : 0 : return;
7131 : : }
7132 : :
7133 : : /* Do two passes - one to verify that we can obtain enough clusters
7134 : : * and another to actually claim them.
7135 : : */
7136 : 175 : clusters_needed = 0;
7137 [ + + ]: 2012 : for (i = 0; i < _blob->active.num_clusters; i++) {
7138 [ + + + + ]: 1837 : if (bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) {
7139 : 1384 : clusters_needed++;
7140 : : }
7141 : : }
7142 : :
7143 [ - + ]: 175 : if (clusters_needed > _blob->bs->num_free_clusters) {
7144 : : /* Not enough free clusters. Cannot satisfy the request. */
7145 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC);
7146 : 0 : return;
7147 : : }
7148 : :
7149 : 175 : ctx->cluster = 0;
7150 : 175 : bs_inflate_blob_touch_next(ctx, 0);
7151 : : }
7152 : :
7153 : : static void
7154 : 188 : bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7155 : : spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg)
7156 : : {
7157 : 188 : struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
7158 : :
7159 [ - + ]: 188 : if (!ctx) {
7160 : 0 : cb_fn(cb_arg, -ENOMEM);
7161 : 0 : return;
7162 : : }
7163 : 188 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
7164 : 188 : ctx->cpl.u.bs_basic.cb_fn = cb_fn;
7165 : 188 : ctx->cpl.u.bs_basic.cb_arg = cb_arg;
7166 : 188 : ctx->bserrno = 0;
7167 : 188 : ctx->original.id = blobid;
7168 : 188 : ctx->channel = channel;
7169 : 188 : ctx->allocate_all = allocate_all;
7170 : :
7171 : 188 : spdk_bs_open_blob(bs, ctx->original.id, bs_inflate_blob_open_cpl, ctx);
7172 : : }
7173 : :
7174 : : void
7175 : 89 : spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7176 : : spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
7177 : : {
7178 : 89 : bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg);
7179 : 89 : }
7180 : :
7181 : : void
7182 : 99 : spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7183 : : spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
7184 : : {
7185 : 99 : bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg);
7186 : 99 : }
7187 : : /* END spdk_bs_inflate_blob */
7188 : :
7189 : : /* START spdk_bs_blob_shallow_copy */
7190 : :
7191 : : struct shallow_copy_ctx {
7192 : : struct spdk_bs_cpl cpl;
7193 : : int bserrno;
7194 : :
7195 : : /* Blob source for copy */
7196 : : struct spdk_blob_store *bs;
7197 : : spdk_blob_id blobid;
7198 : : struct spdk_blob *blob;
7199 : : struct spdk_io_channel *blob_channel;
7200 : :
7201 : : /* Destination device for copy */
7202 : : struct spdk_bs_dev *ext_dev;
7203 : : struct spdk_io_channel *ext_channel;
7204 : :
7205 : : /* Current cluster for copy operation */
7206 : : uint64_t cluster;
7207 : :
7208 : : /* Buffer for blob reading */
7209 : : uint8_t *read_buff;
7210 : :
7211 : : /* Struct for external device writing */
7212 : : struct spdk_bs_dev_cb_args ext_args;
7213 : :
7214 : : /* Actual number of copied clusters */
7215 : : uint64_t copied_clusters_count;
7216 : :
7217 : : /* Status callback for updates about the ongoing operation */
7218 : : spdk_blob_shallow_copy_status status_cb;
7219 : :
7220 : : /* Argument passed to function status_cb */
7221 : : void *status_cb_arg;
7222 : : };
7223 : :
7224 : : static void
7225 : 49 : bs_shallow_copy_cleanup_finish(void *cb_arg, int bserrno)
7226 : : {
7227 : 49 : struct shallow_copy_ctx *ctx = cb_arg;
7228 : 49 : struct spdk_bs_cpl *cpl = &ctx->cpl;
7229 : :
7230 [ - + ]: 49 : if (bserrno != 0) {
7231 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, cleanup error %d\n", ctx->blob->id, bserrno);
7232 : 0 : ctx->bserrno = bserrno;
7233 : : }
7234 : :
7235 : 49 : ctx->ext_dev->destroy_channel(ctx->ext_dev, ctx->ext_channel);
7236 : 49 : spdk_free(ctx->read_buff);
7237 : :
7238 : 49 : cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
7239 : :
7240 : 49 : free(ctx);
7241 : 49 : }
7242 : :
7243 : : static void
7244 : 26 : bs_shallow_copy_bdev_write_cpl(struct spdk_io_channel *channel, void *cb_arg, int bserrno)
7245 : : {
7246 : 26 : struct shallow_copy_ctx *ctx = cb_arg;
7247 : 26 : struct spdk_blob *_blob = ctx->blob;
7248 : :
7249 [ - + ]: 26 : if (bserrno != 0) {
7250 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, ext dev write error %d\n", ctx->blob->id, bserrno);
7251 : 0 : ctx->bserrno = bserrno;
7252 : 0 : _blob->locked_operation_in_progress = false;
7253 : 0 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7254 : 0 : return;
7255 : : }
7256 : :
7257 : 26 : ctx->cluster++;
7258 [ + - ]: 26 : if (ctx->status_cb) {
7259 : 26 : ctx->copied_clusters_count++;
7260 : 26 : ctx->status_cb(ctx->copied_clusters_count, ctx->status_cb_arg);
7261 : : }
7262 : :
7263 : 26 : bs_shallow_copy_cluster_find_next(ctx);
7264 : : }
7265 : :
7266 : : static void
7267 : 26 : bs_shallow_copy_blob_read_cpl(void *cb_arg, int bserrno)
7268 : : {
7269 : 26 : struct shallow_copy_ctx *ctx = cb_arg;
7270 : 26 : struct spdk_bs_dev *ext_dev = ctx->ext_dev;
7271 : 26 : struct spdk_blob *_blob = ctx->blob;
7272 : :
7273 [ - + ]: 26 : if (bserrno != 0) {
7274 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob read error %d\n", ctx->blob->id, bserrno);
7275 : 0 : ctx->bserrno = bserrno;
7276 : 0 : _blob->locked_operation_in_progress = false;
7277 : 0 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7278 : 0 : return;
7279 : : }
7280 : :
7281 : 26 : ctx->ext_args.channel = ctx->ext_channel;
7282 : 26 : ctx->ext_args.cb_fn = bs_shallow_copy_bdev_write_cpl;
7283 : 26 : ctx->ext_args.cb_arg = ctx;
7284 : :
7285 : 44 : ext_dev->write(ext_dev, ctx->ext_channel, ctx->read_buff,
7286 : 26 : bs_cluster_to_lba(_blob->bs, ctx->cluster),
7287 : 26 : bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
7288 : : &ctx->ext_args);
7289 : : }
7290 : :
7291 : : static void
7292 : 39 : bs_shallow_copy_cluster_find_next(void *cb_arg)
7293 : : {
7294 : 39 : struct shallow_copy_ctx *ctx = cb_arg;
7295 : 39 : struct spdk_blob *_blob = ctx->blob;
7296 : :
7297 [ + + ]: 65 : while (ctx->cluster < _blob->active.num_clusters) {
7298 [ + + ]: 52 : if (_blob->active.clusters[ctx->cluster] != 0) {
7299 : 26 : break;
7300 : : }
7301 : :
7302 : 26 : ctx->cluster++;
7303 : : }
7304 : :
7305 [ + + ]: 39 : if (ctx->cluster < _blob->active.num_clusters) {
7306 : 44 : blob_request_submit_op_single(ctx->blob_channel, _blob, ctx->read_buff,
7307 : 26 : bs_cluster_to_lba(_blob->bs, ctx->cluster),
7308 : 26 : bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
7309 : : bs_shallow_copy_blob_read_cpl, ctx, SPDK_BLOB_READ);
7310 : : } else {
7311 : 13 : _blob->locked_operation_in_progress = false;
7312 : 13 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7313 : : }
7314 : 39 : }
7315 : :
7316 : : static void
7317 : 49 : bs_shallow_copy_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
7318 : : {
7319 : 49 : struct shallow_copy_ctx *ctx = cb_arg;
7320 : 49 : struct spdk_bs_dev *ext_dev = ctx->ext_dev;
7321 : : uint32_t blob_block_size;
7322 : : uint64_t blob_total_size;
7323 : :
7324 [ - + ]: 49 : if (bserrno != 0) {
7325 : 0 : SPDK_ERRLOG("Shallow copy blob open error %d\n", bserrno);
7326 : 0 : ctx->bserrno = bserrno;
7327 : 0 : bs_shallow_copy_cleanup_finish(ctx, 0);
7328 : 0 : return;
7329 : : }
7330 : :
7331 [ + + ]: 49 : if (!spdk_blob_is_read_only(_blob)) {
7332 : 12 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob must be read only\n", _blob->id);
7333 : 12 : ctx->bserrno = -EPERM;
7334 : 12 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7335 : 12 : return;
7336 : : }
7337 : :
7338 : 37 : blob_block_size = _blob->bs->dev->blocklen;
7339 : 37 : blob_total_size = spdk_blob_get_num_clusters(_blob) * spdk_bs_get_cluster_size(_blob->bs);
7340 : :
7341 [ + + ]: 37 : if (blob_total_size > ext_dev->blockcnt * ext_dev->blocklen) {
7342 : 12 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device must have at least blob size\n",
7343 : : _blob->id);
7344 : 12 : ctx->bserrno = -EINVAL;
7345 : 12 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7346 : 12 : return;
7347 : : }
7348 : :
7349 [ + + + + ]: 25 : if (blob_block_size % ext_dev->blocklen != 0) {
7350 : 12 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device block size is not compatible with \
7351 : : blobstore block size\n", _blob->id);
7352 : 12 : ctx->bserrno = -EINVAL;
7353 : 12 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7354 : 12 : return;
7355 : : }
7356 : :
7357 : 13 : ctx->blob = _blob;
7358 : :
7359 [ - + - + ]: 13 : if (_blob->locked_operation_in_progress) {
7360 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "blob 0x%" PRIx64 " shallow copy - another operation in progress\n", _blob->id);
7361 : 0 : ctx->bserrno = -EBUSY;
7362 : 0 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7363 : 0 : return;
7364 : : }
7365 : :
7366 : 13 : _blob->locked_operation_in_progress = true;
7367 : :
7368 : 13 : ctx->cluster = 0;
7369 : 13 : bs_shallow_copy_cluster_find_next(ctx);
7370 : : }
7371 : :
7372 : : int
7373 : 49 : spdk_bs_blob_shallow_copy(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7374 : : spdk_blob_id blobid, struct spdk_bs_dev *ext_dev,
7375 : : spdk_blob_shallow_copy_status status_cb_fn, void *status_cb_arg,
7376 : : spdk_blob_op_complete cb_fn, void *cb_arg)
7377 : : {
7378 : : struct shallow_copy_ctx *ctx;
7379 : : struct spdk_io_channel *ext_channel;
7380 : :
7381 : 49 : ctx = calloc(1, sizeof(*ctx));
7382 [ - + ]: 49 : if (!ctx) {
7383 : 0 : return -ENOMEM;
7384 : : }
7385 : :
7386 : 49 : ctx->bs = bs;
7387 : 49 : ctx->blobid = blobid;
7388 : 49 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
7389 : 49 : ctx->cpl.u.bs_basic.cb_fn = cb_fn;
7390 : 49 : ctx->cpl.u.bs_basic.cb_arg = cb_arg;
7391 : 49 : ctx->bserrno = 0;
7392 : 49 : ctx->blob_channel = channel;
7393 : 49 : ctx->status_cb = status_cb_fn;
7394 : 49 : ctx->status_cb_arg = status_cb_arg;
7395 : 49 : ctx->read_buff = spdk_malloc(bs->cluster_sz, bs->dev->blocklen, NULL,
7396 : : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
7397 [ - + ]: 49 : if (!ctx->read_buff) {
7398 : 0 : free(ctx);
7399 : 0 : return -ENOMEM;
7400 : : }
7401 : :
7402 : 49 : ext_channel = ext_dev->create_channel(ext_dev);
7403 [ - + ]: 49 : if (!ext_channel) {
7404 : 0 : spdk_free(ctx->read_buff);
7405 : 0 : free(ctx);
7406 : 0 : return -ENOMEM;
7407 : : }
7408 : 49 : ctx->ext_dev = ext_dev;
7409 : 49 : ctx->ext_channel = ext_channel;
7410 : :
7411 : 49 : spdk_bs_open_blob(ctx->bs, ctx->blobid, bs_shallow_copy_blob_open_cpl, ctx);
7412 : :
7413 : 49 : return 0;
7414 : : }
7415 : : /* END spdk_bs_blob_shallow_copy */
7416 : :
7417 : : /* START spdk_bs_blob_set_parent */
7418 : :
7419 : : struct set_parent_ctx {
7420 : : struct spdk_blob_store *bs;
7421 : : int bserrno;
7422 : : spdk_bs_op_complete cb_fn;
7423 : : void *cb_arg;
7424 : :
7425 : : struct spdk_blob *blob;
7426 : : bool blob_md_ro;
7427 : :
7428 : : struct blob_parent parent;
7429 : : };
7430 : :
7431 : : static void
7432 : 76 : bs_set_parent_cleanup_finish(void *cb_arg, int bserrno)
7433 : : {
7434 : 76 : struct set_parent_ctx *ctx = cb_arg;
7435 : :
7436 [ - + ]: 76 : assert(ctx != NULL);
7437 : :
7438 [ - + ]: 76 : if (bserrno != 0) {
7439 : 0 : SPDK_ERRLOG("blob set parent finish error %d\n", bserrno);
7440 [ # # ]: 0 : if (ctx->bserrno == 0) {
7441 : 0 : ctx->bserrno = bserrno;
7442 : : }
7443 : : }
7444 : :
7445 : 76 : ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
7446 : :
7447 : 76 : free(ctx);
7448 : 76 : }
7449 : :
7450 : : static void
7451 : 63 : bs_set_parent_close_snapshot(void *cb_arg, int bserrno)
7452 : : {
7453 : 63 : struct set_parent_ctx *ctx = cb_arg;
7454 : :
7455 [ + + ]: 63 : if (ctx->bserrno != 0) {
7456 : 24 : spdk_blob_close(ctx->parent.u.snapshot.blob, bs_set_parent_cleanup_finish, ctx);
7457 : 24 : return;
7458 : : }
7459 : :
7460 [ - + ]: 39 : if (bserrno != 0) {
7461 : 0 : SPDK_ERRLOG("blob close error %d\n", bserrno);
7462 : 0 : ctx->bserrno = bserrno;
7463 : : }
7464 : :
7465 : 39 : bs_set_parent_cleanup_finish(ctx, ctx->bserrno);
7466 : : }
7467 : :
7468 : : static void
7469 : 39 : bs_set_parent_close_blob(void *cb_arg, int bserrno)
7470 : : {
7471 : 39 : struct set_parent_ctx *ctx = cb_arg;
7472 : 39 : struct spdk_blob *blob = ctx->blob;
7473 : 39 : struct spdk_blob *snapshot = ctx->parent.u.snapshot.blob;
7474 : :
7475 [ - + - - ]: 39 : if (bserrno != 0 && ctx->bserrno == 0) {
7476 : 0 : SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
7477 : 0 : ctx->bserrno = bserrno;
7478 : : }
7479 : :
7480 : : /* Revert md_ro to original state */
7481 [ - + ]: 39 : blob->md_ro = ctx->blob_md_ro;
7482 : :
7483 : 39 : blob->locked_operation_in_progress = false;
7484 : 39 : snapshot->locked_operation_in_progress = false;
7485 : :
7486 : 39 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7487 : 39 : }
7488 : :
7489 : : static void
7490 : 39 : bs_set_parent_set_back_bs_dev_done(void *cb_arg, int bserrno)
7491 : : {
7492 : 39 : struct set_parent_ctx *ctx = cb_arg;
7493 : 39 : struct spdk_blob *blob = ctx->blob;
7494 : :
7495 [ - + ]: 39 : if (bserrno != 0) {
7496 : 0 : SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
7497 : 0 : ctx->bserrno = bserrno;
7498 : 0 : bs_set_parent_close_blob(ctx, bserrno);
7499 : 0 : return;
7500 : : }
7501 : :
7502 : 39 : spdk_blob_sync_md(blob, bs_set_parent_close_blob, ctx);
7503 : : }
7504 : :
7505 : : static int
7506 : 39 : bs_set_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
7507 : : {
7508 : : int rc;
7509 : :
7510 : 39 : bs_blob_list_remove(blob);
7511 : :
7512 : 39 : rc = blob_set_xattr(blob, BLOB_SNAPSHOT, &parent->u.snapshot.id, sizeof(spdk_blob_id), true);
7513 [ - + ]: 39 : if (rc != 0) {
7514 : 0 : SPDK_ERRLOG("error %d setting snapshot xattr\n", rc);
7515 : 0 : return rc;
7516 : : }
7517 : 39 : blob->parent_id = parent->u.snapshot.id;
7518 : :
7519 [ + + ]: 39 : if (blob_is_esnap_clone(blob)) {
7520 : : /* Remove the xattr that references the external snapshot */
7521 : 13 : blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
7522 : 13 : blob_remove_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
7523 : : }
7524 : :
7525 : 39 : bs_blob_list_add(blob);
7526 : :
7527 : 39 : return 0;
7528 : : }
7529 : :
7530 : : static void
7531 : 64 : bs_set_parent_snapshot_open_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
7532 : : {
7533 : 64 : struct set_parent_ctx *ctx = cb_arg;
7534 : 64 : struct spdk_blob *blob = ctx->blob;
7535 : : struct spdk_bs_dev *back_bs_dev;
7536 : :
7537 [ + + ]: 64 : if (bserrno != 0) {
7538 : 1 : SPDK_ERRLOG("snapshot open error %d\n", bserrno);
7539 : 1 : ctx->bserrno = bserrno;
7540 : 1 : spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
7541 : 1 : return;
7542 : : }
7543 : :
7544 : 63 : ctx->parent.u.snapshot.blob = snapshot;
7545 : 63 : ctx->parent.u.snapshot.id = snapshot->id;
7546 : :
7547 [ + + ]: 63 : if (!spdk_blob_is_snapshot(snapshot)) {
7548 : 12 : SPDK_ERRLOG("parent blob is not a snapshot\n");
7549 : 12 : ctx->bserrno = -EINVAL;
7550 : 12 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7551 : 12 : return;
7552 : : }
7553 : :
7554 [ + + ]: 51 : if (blob->active.num_clusters != snapshot->active.num_clusters) {
7555 : 12 : SPDK_ERRLOG("parent blob has a number of clusters different from child's ones\n");
7556 : 12 : ctx->bserrno = -EINVAL;
7557 : 12 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7558 : 12 : return;
7559 : : }
7560 : :
7561 [ + + + + : 39 : if (blob->locked_operation_in_progress || snapshot->locked_operation_in_progress) {
- + - + ]
7562 : 0 : SPDK_ERRLOG("cannot set parent of blob, another operation in progress\n");
7563 : 0 : ctx->bserrno = -EBUSY;
7564 : 0 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7565 : 0 : return;
7566 : : }
7567 : :
7568 : 39 : blob->locked_operation_in_progress = true;
7569 : 39 : snapshot->locked_operation_in_progress = true;
7570 : :
7571 : : /* Temporarily override md_ro flag for MD modification */
7572 : 39 : blob->md_ro = false;
7573 : :
7574 : 39 : back_bs_dev = bs_create_blob_bs_dev(snapshot);
7575 : :
7576 : 39 : blob_set_back_bs_dev(blob, back_bs_dev, bs_set_parent_refs, &ctx->parent,
7577 : : bs_set_parent_set_back_bs_dev_done,
7578 : : ctx);
7579 : : }
7580 : :
7581 : : static void
7582 : 76 : bs_set_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
7583 : : {
7584 : 76 : struct set_parent_ctx *ctx = cb_arg;
7585 : :
7586 [ - + ]: 76 : if (bserrno != 0) {
7587 : 0 : SPDK_ERRLOG("blob open error %d\n", bserrno);
7588 : 0 : ctx->bserrno = bserrno;
7589 : 0 : bs_set_parent_cleanup_finish(ctx, 0);
7590 : 0 : return;
7591 : : }
7592 : :
7593 [ + + ]: 76 : if (!spdk_blob_is_thin_provisioned(blob)) {
7594 : 12 : SPDK_ERRLOG("blob is not thin-provisioned\n");
7595 : 12 : ctx->bserrno = -EINVAL;
7596 : 12 : spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
7597 : 12 : return;
7598 : : }
7599 : :
7600 : 64 : ctx->blob = blob;
7601 [ - + ]: 64 : ctx->blob_md_ro = blob->md_ro;
7602 : :
7603 : 64 : spdk_bs_open_blob(ctx->bs, ctx->parent.u.snapshot.id, bs_set_parent_snapshot_open_cpl, ctx);
7604 : : }
7605 : :
7606 : : void
7607 : 115 : spdk_bs_blob_set_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
7608 : : spdk_blob_id snapshot_id, spdk_blob_op_complete cb_fn, void *cb_arg)
7609 : : {
7610 : : struct set_parent_ctx *ctx;
7611 : :
7612 [ + + ]: 115 : if (snapshot_id == SPDK_BLOBID_INVALID) {
7613 : 12 : SPDK_ERRLOG("snapshot id not valid\n");
7614 : 12 : cb_fn(cb_arg, -EINVAL);
7615 : 12 : return;
7616 : : }
7617 : :
7618 [ + + ]: 103 : if (blob_id == snapshot_id) {
7619 : 12 : SPDK_ERRLOG("blob id and snapshot id cannot be the same\n");
7620 : 12 : cb_fn(cb_arg, -EINVAL);
7621 : 12 : return;
7622 : : }
7623 : :
7624 [ + + ]: 91 : if (spdk_blob_get_parent_snapshot(bs, blob_id) == snapshot_id) {
7625 : 15 : SPDK_NOTICELOG("snapshot is already the parent of blob\n");
7626 : 15 : cb_fn(cb_arg, -EEXIST);
7627 : 15 : return;
7628 : : }
7629 : :
7630 : 76 : ctx = calloc(1, sizeof(*ctx));
7631 [ - + ]: 76 : if (!ctx) {
7632 : 0 : cb_fn(cb_arg, -ENOMEM);
7633 : 0 : return;
7634 : : }
7635 : :
7636 : 76 : ctx->bs = bs;
7637 : 76 : ctx->parent.u.snapshot.id = snapshot_id;
7638 : 76 : ctx->cb_fn = cb_fn;
7639 : 76 : ctx->cb_arg = cb_arg;
7640 : 76 : ctx->bserrno = 0;
7641 : :
7642 : 76 : spdk_bs_open_blob(bs, blob_id, bs_set_parent_blob_open_cpl, ctx);
7643 : : }
7644 : : /* END spdk_bs_blob_set_parent */
7645 : :
7646 : : /* START spdk_bs_blob_set_external_parent */
7647 : :
7648 : : static void
7649 : 54 : bs_set_external_parent_cleanup_finish(void *cb_arg, int bserrno)
7650 : : {
7651 : 54 : struct set_parent_ctx *ctx = cb_arg;
7652 : :
7653 [ - + ]: 54 : if (bserrno != 0) {
7654 : 0 : SPDK_ERRLOG("blob set external parent finish error %d\n", bserrno);
7655 [ # # ]: 0 : if (ctx->bserrno == 0) {
7656 : 0 : ctx->bserrno = bserrno;
7657 : : }
7658 : : }
7659 : :
7660 : 54 : ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
7661 : :
7662 : 54 : free(ctx->parent.u.esnap.id);
7663 : 54 : free(ctx);
7664 : 54 : }
7665 : :
7666 : : static void
7667 : 27 : bs_set_external_parent_close_blob(void *cb_arg, int bserrno)
7668 : : {
7669 : 27 : struct set_parent_ctx *ctx = cb_arg;
7670 : 27 : struct spdk_blob *blob = ctx->blob;
7671 : :
7672 [ - + - - ]: 27 : if (bserrno != 0 && ctx->bserrno == 0) {
7673 : 0 : SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
7674 : 0 : ctx->bserrno = bserrno;
7675 : : }
7676 : :
7677 : : /* Revert md_ro to original state */
7678 [ - + ]: 27 : blob->md_ro = ctx->blob_md_ro;
7679 : :
7680 : 27 : blob->locked_operation_in_progress = false;
7681 : :
7682 : 27 : spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
7683 : 27 : }
7684 : :
7685 : : static void
7686 : 27 : bs_set_external_parent_unfrozen(void *cb_arg, int bserrno)
7687 : : {
7688 : 27 : struct set_parent_ctx *ctx = cb_arg;
7689 : 27 : struct spdk_blob *blob = ctx->blob;
7690 : :
7691 [ - + ]: 27 : if (bserrno != 0) {
7692 : 0 : SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
7693 : 0 : ctx->bserrno = bserrno;
7694 : 0 : bs_set_external_parent_close_blob(ctx, bserrno);
7695 : 0 : return;
7696 : : }
7697 : :
7698 : 27 : spdk_blob_sync_md(blob, bs_set_external_parent_close_blob, ctx);
7699 : : }
7700 : :
7701 : : static int
7702 : 27 : bs_set_external_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
7703 : : {
7704 : : int rc;
7705 : :
7706 : 27 : bs_blob_list_remove(blob);
7707 : :
7708 [ - + ]: 27 : if (spdk_blob_is_clone(blob)) {
7709 : : /* Remove the xattr that references the snapshot */
7710 : 0 : blob->parent_id = SPDK_BLOBID_INVALID;
7711 : 0 : blob_remove_xattr(blob, BLOB_SNAPSHOT, true);
7712 : : }
7713 : :
7714 : 27 : rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, parent->u.esnap.id,
7715 : 27 : parent->u.esnap.id_len, true);
7716 [ - + ]: 27 : if (rc != 0) {
7717 : 0 : SPDK_ERRLOG("error %d setting external snapshot xattr\n", rc);
7718 : 0 : return rc;
7719 : : }
7720 : 27 : blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
7721 : :
7722 : 27 : bs_blob_list_add(blob);
7723 : :
7724 : 27 : return 0;
7725 : : }
7726 : :
7727 : : static void
7728 : 54 : bs_set_external_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
7729 : : {
7730 : 54 : struct set_parent_ctx *ctx = cb_arg;
7731 : 54 : const void *esnap_id;
7732 : 54 : size_t esnap_id_len;
7733 : : int rc;
7734 : :
7735 [ - + ]: 54 : if (bserrno != 0) {
7736 : 0 : SPDK_ERRLOG("blob open error %d\n", bserrno);
7737 : 0 : ctx->bserrno = bserrno;
7738 : 0 : bs_set_parent_cleanup_finish(ctx, 0);
7739 : 0 : return;
7740 : : }
7741 : :
7742 : 54 : ctx->blob = blob;
7743 [ - + ]: 54 : ctx->blob_md_ro = blob->md_ro;
7744 : :
7745 : 54 : rc = spdk_blob_get_esnap_id(blob, &esnap_id, &esnap_id_len);
7746 [ + + + - : 54 : if (rc == 0 && esnap_id != NULL && esnap_id_len == ctx->parent.u.esnap.id_len &&
+ - ]
7747 [ + + - + : 16 : memcmp(esnap_id, ctx->parent.u.esnap.id, esnap_id_len) == 0) {
+ + ]
7748 : 15 : SPDK_ERRLOG("external snapshot is already the parent of blob\n");
7749 : 15 : ctx->bserrno = -EEXIST;
7750 : 15 : goto error;
7751 : : }
7752 : :
7753 [ + + ]: 39 : if (!spdk_blob_is_thin_provisioned(blob)) {
7754 : 12 : SPDK_ERRLOG("blob is not thin-provisioned\n");
7755 : 12 : ctx->bserrno = -EINVAL;
7756 : 12 : goto error;
7757 : : }
7758 : :
7759 [ - + - + ]: 27 : if (blob->locked_operation_in_progress) {
7760 : 0 : SPDK_ERRLOG("cannot set external parent of blob, another operation in progress\n");
7761 : 0 : ctx->bserrno = -EBUSY;
7762 : 0 : goto error;
7763 : : }
7764 : :
7765 : 27 : blob->locked_operation_in_progress = true;
7766 : :
7767 : : /* Temporarily override md_ro flag for MD modification */
7768 : 27 : blob->md_ro = false;
7769 : :
7770 : 27 : blob_set_back_bs_dev(blob, ctx->parent.u.esnap.back_bs_dev, bs_set_external_parent_refs,
7771 : : &ctx->parent, bs_set_external_parent_unfrozen, ctx);
7772 : 27 : return;
7773 : :
7774 : 27 : error:
7775 : 27 : spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
7776 : : }
7777 : :
7778 : : void
7779 : 78 : spdk_bs_blob_set_external_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
7780 : : struct spdk_bs_dev *esnap_bs_dev, const void *esnap_id,
7781 : : uint32_t esnap_id_len, spdk_blob_op_complete cb_fn, void *cb_arg)
7782 : : {
7783 : : struct set_parent_ctx *ctx;
7784 : : uint64_t esnap_dev_size, cluster_sz;
7785 : :
7786 [ + + + + : 78 : if (sizeof(blob_id) == esnap_id_len && memcmp(&blob_id, esnap_id, sizeof(blob_id)) == 0) {
+ - ]
7787 : 12 : SPDK_ERRLOG("blob id and external snapshot id cannot be the same\n");
7788 : 12 : cb_fn(cb_arg, -EINVAL);
7789 : 12 : return;
7790 : : }
7791 : :
7792 : 66 : esnap_dev_size = esnap_bs_dev->blockcnt * esnap_bs_dev->blocklen;
7793 : 66 : cluster_sz = spdk_bs_get_cluster_size(bs);
7794 [ + + + + ]: 66 : if ((esnap_dev_size % cluster_sz) != 0) {
7795 : 12 : SPDK_ERRLOG("Esnap device size %" PRIu64 " is not an integer multiple of "
7796 : : "cluster size %" PRIu64 "\n", esnap_dev_size, cluster_sz);
7797 : 12 : cb_fn(cb_arg, -EINVAL);
7798 : 12 : return;
7799 : : }
7800 : :
7801 : 54 : ctx = calloc(1, sizeof(*ctx));
7802 [ - + ]: 54 : if (!ctx) {
7803 : 0 : cb_fn(cb_arg, -ENOMEM);
7804 : 0 : return;
7805 : : }
7806 : :
7807 : 54 : ctx->parent.u.esnap.id = calloc(1, esnap_id_len);
7808 [ - + ]: 54 : if (!ctx->parent.u.esnap.id) {
7809 : 0 : free(ctx);
7810 : 0 : cb_fn(cb_arg, -ENOMEM);
7811 : 0 : return;
7812 : : }
7813 : :
7814 : 54 : ctx->bs = bs;
7815 : 54 : ctx->parent.u.esnap.back_bs_dev = esnap_bs_dev;
7816 [ - + - + ]: 54 : memcpy(ctx->parent.u.esnap.id, esnap_id, esnap_id_len);
7817 : 54 : ctx->parent.u.esnap.id_len = esnap_id_len;
7818 : 54 : ctx->cb_fn = cb_fn;
7819 : 54 : ctx->cb_arg = cb_arg;
7820 : 54 : ctx->bserrno = 0;
7821 : :
7822 : 54 : spdk_bs_open_blob(bs, blob_id, bs_set_external_parent_blob_open_cpl, ctx);
7823 : : }
7824 : : /* END spdk_bs_blob_set_external_parent */
7825 : :
7826 : : /* START spdk_blob_resize */
7827 : : struct spdk_bs_resize_ctx {
7828 : : spdk_blob_op_complete cb_fn;
7829 : : void *cb_arg;
7830 : : struct spdk_blob *blob;
7831 : : uint64_t sz;
7832 : : int rc;
7833 : : };
7834 : :
7835 : : static void
7836 : 27270 : bs_resize_unfreeze_cpl(void *cb_arg, int rc)
7837 : : {
7838 : 27270 : struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
7839 : :
7840 [ - + ]: 27270 : if (rc != 0) {
7841 : 0 : SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc);
7842 : : }
7843 : :
7844 [ + + ]: 27270 : if (ctx->rc != 0) {
7845 : 13 : SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc);
7846 : 13 : rc = ctx->rc;
7847 : : }
7848 : :
7849 : 27270 : ctx->blob->locked_operation_in_progress = false;
7850 : :
7851 : 27270 : ctx->cb_fn(ctx->cb_arg, rc);
7852 : 27270 : free(ctx);
7853 : 27270 : }
7854 : :
7855 : : static void
7856 : 27270 : bs_resize_freeze_cpl(void *cb_arg, int rc)
7857 : : {
7858 : 27270 : struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
7859 : :
7860 [ - + ]: 27270 : if (rc != 0) {
7861 : 0 : ctx->blob->locked_operation_in_progress = false;
7862 : 0 : ctx->cb_fn(ctx->cb_arg, rc);
7863 : 0 : free(ctx);
7864 : 0 : return;
7865 : : }
7866 : :
7867 : 27270 : ctx->rc = blob_resize(ctx->blob, ctx->sz);
7868 : :
7869 : 27270 : blob_unfreeze_io(ctx->blob, bs_resize_unfreeze_cpl, ctx);
7870 : : }
7871 : :
7872 : : void
7873 : 27312 : spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg)
7874 : : {
7875 : : struct spdk_bs_resize_ctx *ctx;
7876 : :
7877 : 27312 : blob_verify_md_op(blob);
7878 : :
7879 [ - + - + ]: 27312 : SPDK_DEBUGLOG(blob, "Resizing blob 0x%" PRIx64 " to %" PRIu64 " clusters\n", blob->id, sz);
7880 : :
7881 [ + + + + ]: 27312 : if (blob->md_ro) {
7882 : 12 : cb_fn(cb_arg, -EPERM);
7883 : 12 : return;
7884 : : }
7885 : :
7886 [ + + ]: 27300 : if (sz == blob->active.num_clusters) {
7887 : 30 : cb_fn(cb_arg, 0);
7888 : 30 : return;
7889 : : }
7890 : :
7891 [ - + - + ]: 27270 : if (blob->locked_operation_in_progress) {
7892 : 0 : cb_fn(cb_arg, -EBUSY);
7893 : 0 : return;
7894 : : }
7895 : :
7896 : 27270 : ctx = calloc(1, sizeof(*ctx));
7897 [ - + ]: 27270 : if (!ctx) {
7898 : 0 : cb_fn(cb_arg, -ENOMEM);
7899 : 0 : return;
7900 : : }
7901 : :
7902 : 27270 : blob->locked_operation_in_progress = true;
7903 : 27270 : ctx->cb_fn = cb_fn;
7904 : 27270 : ctx->cb_arg = cb_arg;
7905 : 27270 : ctx->blob = blob;
7906 : 27270 : ctx->sz = sz;
7907 : 27270 : blob_freeze_io(blob, bs_resize_freeze_cpl, ctx);
7908 : : }
7909 : :
7910 : : /* END spdk_blob_resize */
7911 : :
7912 : :
7913 : : /* START spdk_bs_delete_blob */
7914 : :
7915 : : static void
7916 : 5209 : bs_delete_close_cpl(void *cb_arg, int bserrno)
7917 : : {
7918 : 5209 : spdk_bs_sequence_t *seq = cb_arg;
7919 : :
7920 : 5209 : bs_sequence_finish(seq, bserrno);
7921 : 5209 : }
7922 : :
7923 : : static void
7924 : 5209 : bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
7925 : : {
7926 : 5209 : struct spdk_blob *blob = cb_arg;
7927 : :
7928 [ - + ]: 5209 : if (bserrno != 0) {
7929 : : /*
7930 : : * We already removed this blob from the blobstore tailq, so
7931 : : * we need to free it here since this is the last reference
7932 : : * to it.
7933 : : */
7934 : 0 : blob_free(blob);
7935 : 0 : bs_delete_close_cpl(seq, bserrno);
7936 : 0 : return;
7937 : : }
7938 : :
7939 : : /*
7940 : : * This will immediately decrement the ref_count and call
7941 : : * the completion routine since the metadata state is clean.
7942 : : * By calling spdk_blob_close, we reduce the number of call
7943 : : * points into code that touches the blob->open_ref count
7944 : : * and the blobstore's blob list.
7945 : : */
7946 : 5209 : spdk_blob_close(blob, bs_delete_close_cpl, seq);
7947 : : }
7948 : :
7949 : : struct delete_snapshot_ctx {
7950 : : struct spdk_blob_list *parent_snapshot_entry;
7951 : : struct spdk_blob *snapshot;
7952 : : struct spdk_blob_md_page *page;
7953 : : bool snapshot_md_ro;
7954 : : struct spdk_blob *clone;
7955 : : bool clone_md_ro;
7956 : : spdk_blob_op_with_handle_complete cb_fn;
7957 : : void *cb_arg;
7958 : : int bserrno;
7959 : : uint32_t next_extent_page;
7960 : : };
7961 : :
7962 : : static void
7963 : 338 : delete_blob_cleanup_finish(void *cb_arg, int bserrno)
7964 : : {
7965 : 338 : struct delete_snapshot_ctx *ctx = cb_arg;
7966 : :
7967 [ - + ]: 338 : if (bserrno != 0) {
7968 : 0 : SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno);
7969 : : }
7970 : :
7971 [ - + ]: 338 : assert(ctx != NULL);
7972 : :
7973 [ - + - - ]: 338 : if (bserrno != 0 && ctx->bserrno == 0) {
7974 : 0 : ctx->bserrno = bserrno;
7975 : : }
7976 : :
7977 : 338 : ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno);
7978 : 338 : spdk_free(ctx->page);
7979 : 338 : free(ctx);
7980 : 338 : }
7981 : :
7982 : : static void
7983 : 66 : delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno)
7984 : : {
7985 : 66 : struct delete_snapshot_ctx *ctx = cb_arg;
7986 : :
7987 [ - + ]: 66 : if (bserrno != 0) {
7988 : 0 : ctx->bserrno = bserrno;
7989 : 0 : SPDK_ERRLOG("Clone cleanup error %d\n", bserrno);
7990 : : }
7991 : :
7992 [ + - ]: 66 : if (ctx->bserrno != 0) {
7993 [ - + ]: 66 : assert(blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL);
7994 : 66 : RB_INSERT(spdk_blob_tree, &ctx->snapshot->bs->open_blobs, ctx->snapshot);
7995 : 66 : spdk_bit_array_set(ctx->snapshot->bs->open_blobids, ctx->snapshot->id);
7996 : : }
7997 : :
7998 : 66 : ctx->snapshot->locked_operation_in_progress = false;
7999 [ - + ]: 66 : ctx->snapshot->md_ro = ctx->snapshot_md_ro;
8000 : :
8001 : 66 : spdk_blob_close(ctx->snapshot, delete_blob_cleanup_finish, ctx);
8002 : 66 : }
8003 : :
8004 : : static void
8005 : 36 : delete_snapshot_cleanup_clone(void *cb_arg, int bserrno)
8006 : : {
8007 : 36 : struct delete_snapshot_ctx *ctx = cb_arg;
8008 : :
8009 : 36 : ctx->clone->locked_operation_in_progress = false;
8010 [ - + ]: 36 : ctx->clone->md_ro = ctx->clone_md_ro;
8011 : :
8012 : 36 : spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
8013 : 36 : }
8014 : :
8015 : : static void
8016 : 152 : delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
8017 : : {
8018 : 152 : struct delete_snapshot_ctx *ctx = cb_arg;
8019 : :
8020 [ - + ]: 152 : if (bserrno) {
8021 : 0 : ctx->bserrno = bserrno;
8022 : 0 : delete_snapshot_cleanup_clone(ctx, 0);
8023 : 0 : return;
8024 : : }
8025 : :
8026 : 152 : ctx->clone->locked_operation_in_progress = false;
8027 : 152 : spdk_blob_close(ctx->clone, delete_blob_cleanup_finish, ctx);
8028 : : }
8029 : :
8030 : : static void
8031 : 164 : delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno)
8032 : : {
8033 : 164 : struct delete_snapshot_ctx *ctx = cb_arg;
8034 : 164 : struct spdk_blob_list *parent_snapshot_entry = NULL;
8035 : 164 : struct spdk_blob_list *snapshot_entry = NULL;
8036 : 164 : struct spdk_blob_list *clone_entry = NULL;
8037 : 164 : struct spdk_blob_list *snapshot_clone_entry = NULL;
8038 : :
8039 [ + + ]: 164 : if (bserrno) {
8040 : 12 : SPDK_ERRLOG("Failed to sync MD on blob\n");
8041 : 12 : ctx->bserrno = bserrno;
8042 : 12 : delete_snapshot_cleanup_clone(ctx, 0);
8043 : 12 : return;
8044 : : }
8045 : :
8046 : : /* Get snapshot entry for the snapshot we want to remove */
8047 : 152 : snapshot_entry = bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id);
8048 : :
8049 [ - + ]: 152 : assert(snapshot_entry != NULL);
8050 : :
8051 : : /* Remove clone entry in this snapshot (at this point there can be only one clone) */
8052 : 152 : clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
8053 [ - + ]: 152 : assert(clone_entry != NULL);
8054 [ - + ]: 152 : TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
8055 : 152 : snapshot_entry->clone_count--;
8056 [ - + ]: 152 : assert(TAILQ_EMPTY(&snapshot_entry->clones));
8057 : :
8058 [ + + ]: 152 : switch (ctx->snapshot->parent_id) {
8059 : 127 : case SPDK_BLOBID_INVALID:
8060 : : case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
8061 : : /* No parent snapshot - just remove clone entry */
8062 : 127 : free(clone_entry);
8063 : 127 : break;
8064 : 25 : default:
8065 : : /* This snapshot is at the same time a clone of another snapshot - we need to
8066 : : * update parent snapshot (remove current clone, add new one inherited from
8067 : : * the snapshot that is being removed) */
8068 : :
8069 : : /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
8070 : : * snapshot that we are removing */
8071 : 25 : blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry,
8072 : : &snapshot_clone_entry);
8073 : :
8074 : : /* Switch clone entry in parent snapshot */
8075 : 25 : TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link);
8076 [ + - ]: 25 : TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link);
8077 : 25 : free(snapshot_clone_entry);
8078 : : }
8079 : :
8080 : : /* Restore md_ro flags */
8081 [ - + ]: 152 : ctx->clone->md_ro = ctx->clone_md_ro;
8082 [ - + ]: 152 : ctx->snapshot->md_ro = ctx->snapshot_md_ro;
8083 : :
8084 : 152 : blob_unfreeze_io(ctx->clone, delete_snapshot_unfreeze_cpl, ctx);
8085 : : }
8086 : :
8087 : : static void
8088 : 176 : delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno)
8089 : : {
8090 : 176 : struct delete_snapshot_ctx *ctx = cb_arg;
8091 : : uint64_t i;
8092 : :
8093 : 176 : ctx->snapshot->md_ro = false;
8094 : :
8095 [ + + ]: 176 : if (bserrno) {
8096 : 12 : SPDK_ERRLOG("Failed to sync MD on clone\n");
8097 : 12 : ctx->bserrno = bserrno;
8098 : :
8099 : : /* Restore snapshot to previous state */
8100 : 12 : bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
8101 [ - + ]: 12 : if (bserrno != 0) {
8102 : 0 : delete_snapshot_cleanup_clone(ctx, bserrno);
8103 : 0 : return;
8104 : : }
8105 : :
8106 : 12 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
8107 : 12 : return;
8108 : : }
8109 : :
8110 : : /* Clear cluster map entries for snapshot */
8111 [ + + + - ]: 1889 : for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
8112 [ + + ]: 1725 : if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) {
8113 [ + + ]: 1685 : if (ctx->snapshot->active.clusters[i] != 0) {
8114 : 997 : ctx->snapshot->active.num_allocated_clusters--;
8115 : : }
8116 : 1685 : ctx->snapshot->active.clusters[i] = 0;
8117 : : }
8118 : : }
8119 [ + + ]: 250 : for (i = 0; i < ctx->snapshot->active.num_extent_pages &&
8120 [ + - ]: 172 : i < ctx->clone->active.num_extent_pages; i++) {
8121 [ + + ]: 86 : if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) {
8122 : 76 : ctx->snapshot->active.extent_pages[i] = 0;
8123 : : }
8124 : : }
8125 : :
8126 : 164 : blob_set_thin_provision(ctx->snapshot);
8127 : 164 : ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY;
8128 : :
8129 [ + + ]: 164 : if (ctx->parent_snapshot_entry != NULL) {
8130 : 25 : ctx->snapshot->back_bs_dev = NULL;
8131 : : }
8132 : :
8133 : 164 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_cpl, ctx);
8134 : : }
8135 : :
8136 : : static void
8137 : 176 : delete_snapshot_update_extent_pages_cpl(struct delete_snapshot_ctx *ctx)
8138 : : {
8139 : : int bserrno;
8140 : :
8141 : : /* Delete old backing bs_dev from clone (related to snapshot that will be removed) */
8142 : 176 : blob_back_bs_destroy(ctx->clone);
8143 : :
8144 : : /* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */
8145 [ + + ]: 176 : if (ctx->snapshot->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
8146 : 28 : bserrno = bs_snapshot_copy_xattr(ctx->clone, ctx->snapshot,
8147 : : BLOB_EXTERNAL_SNAPSHOT_ID);
8148 [ - + ]: 28 : if (bserrno != 0) {
8149 : 0 : ctx->bserrno = bserrno;
8150 : :
8151 : : /* Restore snapshot to previous state */
8152 : 0 : bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
8153 [ # # ]: 0 : if (bserrno != 0) {
8154 : 0 : delete_snapshot_cleanup_clone(ctx, bserrno);
8155 : 0 : return;
8156 : : }
8157 : :
8158 : 0 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
8159 : 0 : return;
8160 : : }
8161 : 28 : ctx->clone->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
8162 : 28 : ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
8163 : : /* Do not delete the external snapshot along with this snapshot */
8164 : 28 : ctx->snapshot->back_bs_dev = NULL;
8165 : 28 : ctx->clone->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
8166 [ + + ]: 148 : } else if (ctx->parent_snapshot_entry != NULL) {
8167 : : /* ...to parent snapshot */
8168 : 25 : ctx->clone->parent_id = ctx->parent_snapshot_entry->id;
8169 : 25 : ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
8170 : 25 : blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id,
8171 : : sizeof(spdk_blob_id),
8172 : : true);
8173 : : } else {
8174 : : /* ...to blobid invalid and zeroes dev */
8175 : 123 : ctx->clone->parent_id = SPDK_BLOBID_INVALID;
8176 : 123 : ctx->clone->back_bs_dev = bs_create_zeroes_dev();
8177 : 123 : blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true);
8178 : : }
8179 : :
8180 : 176 : spdk_blob_sync_md(ctx->clone, delete_snapshot_sync_clone_cpl, ctx);
8181 : : }
8182 : :
8183 : : static void
8184 : 186 : delete_snapshot_update_extent_pages(void *cb_arg, int bserrno)
8185 : : {
8186 : 186 : struct delete_snapshot_ctx *ctx = cb_arg;
8187 : : uint32_t *extent_page;
8188 : : uint64_t i;
8189 : :
8190 [ + + ]: 268 : for (i = ctx->next_extent_page; i < ctx->snapshot->active.num_extent_pages &&
8191 [ + - ]: 174 : i < ctx->clone->active.num_extent_pages; i++) {
8192 [ + + ]: 92 : if (ctx->snapshot->active.extent_pages[i] == 0) {
8193 : : /* No extent page to use from snapshot */
8194 : 28 : continue;
8195 : : }
8196 : :
8197 : 64 : extent_page = &ctx->clone->active.extent_pages[i];
8198 [ + + ]: 64 : if (*extent_page == 0) {
8199 : : /* Copy extent page from snapshot when clone did not have a matching one */
8200 : 54 : *extent_page = ctx->snapshot->active.extent_pages[i];
8201 : 54 : continue;
8202 : : }
8203 : :
8204 : : /* Clone and snapshot both contain partially filled matching extent pages.
8205 : : * Update the clone extent page in place with cluster map containing the mix of both. */
8206 : 10 : ctx->next_extent_page = i + 1;
8207 [ - + ]: 10 : memset(ctx->page, 0, SPDK_BS_PAGE_SIZE);
8208 : :
8209 : 10 : blob_write_extent_page(ctx->clone, *extent_page, i * SPDK_EXTENTS_PER_EP, ctx->page,
8210 : : delete_snapshot_update_extent_pages, ctx);
8211 : 10 : return;
8212 : : }
8213 : 176 : delete_snapshot_update_extent_pages_cpl(ctx);
8214 : : }
8215 : :
8216 : : static void
8217 : 188 : delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno)
8218 : : {
8219 : 188 : struct delete_snapshot_ctx *ctx = cb_arg;
8220 : : uint64_t i;
8221 : :
8222 : : /* Temporarily override md_ro flag for clone for MD modification */
8223 [ - + ]: 188 : ctx->clone_md_ro = ctx->clone->md_ro;
8224 : 188 : ctx->clone->md_ro = false;
8225 : :
8226 [ + + ]: 188 : if (bserrno) {
8227 : 12 : SPDK_ERRLOG("Failed to sync MD with xattr on blob\n");
8228 : 12 : ctx->bserrno = bserrno;
8229 : 12 : delete_snapshot_cleanup_clone(ctx, 0);
8230 : 12 : return;
8231 : : }
8232 : :
8233 : : /* Copy snapshot map to clone map (only unallocated clusters in clone) */
8234 [ + + + - ]: 2021 : for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
8235 [ + + ]: 1845 : if (ctx->clone->active.clusters[i] == 0) {
8236 : 1805 : ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i];
8237 [ + + ]: 1805 : if (ctx->clone->active.clusters[i] != 0) {
8238 : 1117 : ctx->clone->active.num_allocated_clusters++;
8239 : : }
8240 : : }
8241 : : }
8242 : 176 : ctx->next_extent_page = 0;
8243 : 176 : delete_snapshot_update_extent_pages(ctx, 0);
8244 : : }
8245 : :
8246 : : static void
8247 : 28 : delete_snapshot_esnap_channels_destroyed_cb(void *cb_arg, struct spdk_blob *blob, int bserrno)
8248 : : {
8249 : 28 : struct delete_snapshot_ctx *ctx = cb_arg;
8250 : :
8251 [ - + ]: 28 : if (bserrno != 0) {
8252 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to destroy esnap channels: %d\n",
8253 : : blob->id, bserrno);
8254 : : /* That error should not stop us from syncing metadata. */
8255 : : }
8256 : :
8257 : 28 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
8258 : 28 : }
8259 : :
8260 : : static void
8261 : 188 : delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno)
8262 : : {
8263 : 188 : struct delete_snapshot_ctx *ctx = cb_arg;
8264 : :
8265 [ - + ]: 188 : if (bserrno) {
8266 : 0 : SPDK_ERRLOG("Failed to freeze I/O on clone\n");
8267 : 0 : ctx->bserrno = bserrno;
8268 : 0 : delete_snapshot_cleanup_clone(ctx, 0);
8269 : 0 : return;
8270 : : }
8271 : :
8272 : : /* Temporarily override md_ro flag for snapshot for MD modification */
8273 [ - + ]: 188 : ctx->snapshot_md_ro = ctx->snapshot->md_ro;
8274 : 188 : ctx->snapshot->md_ro = false;
8275 : :
8276 : : /* Mark blob as pending for removal for power failure safety, use clone id for recovery */
8277 : 188 : ctx->bserrno = blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id,
8278 : : sizeof(spdk_blob_id), true);
8279 [ - + ]: 188 : if (ctx->bserrno != 0) {
8280 : 0 : delete_snapshot_cleanup_clone(ctx, 0);
8281 : 0 : return;
8282 : : }
8283 : :
8284 [ + + ]: 188 : if (blob_is_esnap_clone(ctx->snapshot)) {
8285 : 28 : blob_esnap_destroy_bs_dev_channels(ctx->snapshot, false,
8286 : : delete_snapshot_esnap_channels_destroyed_cb,
8287 : : ctx);
8288 : 28 : return;
8289 : : }
8290 : :
8291 : 160 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
8292 : : }
8293 : :
8294 : : static void
8295 : 218 : delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno)
8296 : : {
8297 : 218 : struct delete_snapshot_ctx *ctx = cb_arg;
8298 : :
8299 [ + + ]: 218 : if (bserrno) {
8300 : 30 : SPDK_ERRLOG("Failed to open clone\n");
8301 : 30 : ctx->bserrno = bserrno;
8302 : 30 : delete_snapshot_cleanup_snapshot(ctx, 0);
8303 : 30 : return;
8304 : : }
8305 : :
8306 : 188 : ctx->clone = clone;
8307 : :
8308 [ - + - + ]: 188 : if (clone->locked_operation_in_progress) {
8309 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress on its clone\n");
8310 : 0 : ctx->bserrno = -EBUSY;
8311 : 0 : spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
8312 : 0 : return;
8313 : : }
8314 : :
8315 : 188 : clone->locked_operation_in_progress = true;
8316 : :
8317 : 188 : blob_freeze_io(clone, delete_snapshot_freeze_io_cb, ctx);
8318 : : }
8319 : :
8320 : : static void
8321 : 218 : update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx)
8322 : : {
8323 : 218 : struct spdk_blob_list *snapshot_entry = NULL;
8324 : 218 : struct spdk_blob_list *clone_entry = NULL;
8325 : 218 : struct spdk_blob_list *snapshot_clone_entry = NULL;
8326 : :
8327 : : /* Get snapshot entry for the snapshot we want to remove */
8328 : 218 : snapshot_entry = bs_get_snapshot_entry(snapshot->bs, snapshot->id);
8329 : :
8330 [ - + ]: 218 : assert(snapshot_entry != NULL);
8331 : :
8332 : : /* Get clone of the snapshot (at this point there can be only one clone) */
8333 : 218 : clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
8334 [ - + ]: 218 : assert(snapshot_entry->clone_count == 1);
8335 [ - + ]: 218 : assert(clone_entry != NULL);
8336 : :
8337 : : /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
8338 : : * snapshot that we are removing */
8339 : 218 : blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry,
8340 : : &snapshot_clone_entry);
8341 : :
8342 : 218 : spdk_bs_open_blob(snapshot->bs, clone_entry->id, delete_snapshot_open_clone_cb, ctx);
8343 : 218 : }
8344 : :
8345 : : static void
8346 : 5395 : bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno)
8347 : : {
8348 : 5395 : spdk_bs_sequence_t *seq = cb_arg;
8349 : 5395 : struct spdk_blob_list *snapshot_entry = NULL;
8350 : : uint32_t page_num;
8351 : :
8352 [ + + ]: 5395 : if (bserrno) {
8353 : 186 : SPDK_ERRLOG("Failed to remove blob\n");
8354 : 186 : bs_sequence_finish(seq, bserrno);
8355 : 186 : return;
8356 : : }
8357 : :
8358 : : /* Remove snapshot from the list */
8359 : 5209 : snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
8360 [ + + ]: 5209 : if (snapshot_entry != NULL) {
8361 [ + + ]: 455 : TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link);
8362 : 455 : free(snapshot_entry);
8363 : : }
8364 : :
8365 : 5209 : page_num = bs_blobid_to_page(blob->id);
8366 : 5209 : spdk_bit_array_clear(blob->bs->used_blobids, page_num);
8367 : 5209 : blob->state = SPDK_BLOB_STATE_DIRTY;
8368 : 5209 : blob->active.num_pages = 0;
8369 : 5209 : blob_resize(blob, 0);
8370 : :
8371 : 5209 : blob_persist(seq, blob, bs_delete_persist_cpl, blob);
8372 : : }
8373 : :
8374 : : static int
8375 : 5395 : bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone)
8376 : : {
8377 : 5395 : struct spdk_blob_list *snapshot_entry = NULL;
8378 : 5395 : struct spdk_blob_list *clone_entry = NULL;
8379 : 5395 : struct spdk_blob *clone = NULL;
8380 : 5395 : bool has_one_clone = false;
8381 : :
8382 : : /* Check if this is a snapshot with clones */
8383 : 5395 : snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
8384 [ + + ]: 5395 : if (snapshot_entry != NULL) {
8385 [ + + ]: 605 : if (snapshot_entry->clone_count > 1) {
8386 : 72 : SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n");
8387 : 72 : return -EBUSY;
8388 [ + + ]: 533 : } else if (snapshot_entry->clone_count == 1) {
8389 : 218 : has_one_clone = true;
8390 : : }
8391 : : }
8392 : :
8393 : : /* Check if someone has this blob open (besides this delete context):
8394 : : * - open_ref = 1 - only this context opened blob, so it is ok to remove it
8395 : : * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot
8396 : : * and that is ok, because we will update it accordingly */
8397 [ + + + + ]: 5323 : if (blob->open_ref <= 2 && has_one_clone) {
8398 : 218 : clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
8399 [ - + ]: 218 : assert(clone_entry != NULL);
8400 : 218 : clone = blob_lookup(blob->bs, clone_entry->id);
8401 : :
8402 [ + + - + ]: 218 : if (blob->open_ref == 2 && clone == NULL) {
8403 : : /* Clone is closed and someone else opened this blob */
8404 : 0 : SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
8405 : 0 : return -EBUSY;
8406 : : }
8407 : :
8408 : 218 : *update_clone = true;
8409 : 218 : return 0;
8410 : : }
8411 : :
8412 [ + + ]: 5105 : if (blob->open_ref > 1) {
8413 : 48 : SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
8414 : 48 : return -EBUSY;
8415 : : }
8416 : :
8417 [ - + ]: 5057 : assert(has_one_clone == false);
8418 : 5057 : *update_clone = false;
8419 : 5057 : return 0;
8420 : : }
8421 : :
8422 : : static void
8423 : 0 : bs_delete_enomem_close_cpl(void *cb_arg, int bserrno)
8424 : : {
8425 : 0 : spdk_bs_sequence_t *seq = cb_arg;
8426 : :
8427 : 0 : bs_sequence_finish(seq, -ENOMEM);
8428 : 0 : }
8429 : :
8430 : : static void
8431 : 5425 : bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
8432 : : {
8433 : 5425 : spdk_bs_sequence_t *seq = cb_arg;
8434 : : struct delete_snapshot_ctx *ctx;
8435 : 5425 : bool update_clone = false;
8436 : :
8437 [ + + ]: 5425 : if (bserrno != 0) {
8438 : 30 : bs_sequence_finish(seq, bserrno);
8439 : 30 : return;
8440 : : }
8441 : :
8442 : 5395 : blob_verify_md_op(blob);
8443 : :
8444 : 5395 : ctx = calloc(1, sizeof(*ctx));
8445 [ - + ]: 5395 : if (ctx == NULL) {
8446 : 0 : spdk_blob_close(blob, bs_delete_enomem_close_cpl, seq);
8447 : 0 : return;
8448 : : }
8449 : :
8450 : 5395 : ctx->snapshot = blob;
8451 : 5395 : ctx->cb_fn = bs_delete_blob_finish;
8452 : 5395 : ctx->cb_arg = seq;
8453 : :
8454 : : /* Check if blob can be removed and if it is a snapshot with clone on top of it */
8455 : 5395 : ctx->bserrno = bs_is_blob_deletable(blob, &update_clone);
8456 [ + + ]: 5395 : if (ctx->bserrno) {
8457 : 120 : spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
8458 : 120 : return;
8459 : : }
8460 : :
8461 [ - + - + ]: 5275 : if (blob->locked_operation_in_progress) {
8462 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress\n");
8463 : 0 : ctx->bserrno = -EBUSY;
8464 : 0 : spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
8465 : 0 : return;
8466 : : }
8467 : :
8468 : 5275 : blob->locked_operation_in_progress = true;
8469 : :
8470 : : /*
8471 : : * Remove the blob from the blob_store list now, to ensure it does not
8472 : : * get returned after this point by blob_lookup().
8473 : : */
8474 : 5275 : spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
8475 : 5275 : RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
8476 : :
8477 [ + + + + ]: 5275 : if (update_clone) {
8478 : 218 : ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
8479 [ - + ]: 218 : if (!ctx->page) {
8480 : 0 : ctx->bserrno = -ENOMEM;
8481 : 0 : spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
8482 : 0 : return;
8483 : : }
8484 : : /* This blob is a snapshot with active clone - update clone first */
8485 : 218 : update_clone_on_snapshot_deletion(blob, ctx);
8486 : : } else {
8487 : : /* This blob does not have any clones - just remove it */
8488 : 5057 : bs_blob_list_remove(blob);
8489 : 5057 : bs_delete_blob_finish(seq, blob, 0);
8490 : 5057 : free(ctx);
8491 : : }
8492 : : }
8493 : :
8494 : : void
8495 : 5425 : spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
8496 : : spdk_blob_op_complete cb_fn, void *cb_arg)
8497 : : {
8498 : 5314 : struct spdk_bs_cpl cpl;
8499 : : spdk_bs_sequence_t *seq;
8500 : :
8501 [ - + - + ]: 5425 : SPDK_DEBUGLOG(blob, "Deleting blob 0x%" PRIx64 "\n", blobid);
8502 : :
8503 [ - + ]: 5425 : assert(spdk_get_thread() == bs->md_thread);
8504 : :
8505 : 5425 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
8506 : 5425 : cpl.u.blob_basic.cb_fn = cb_fn;
8507 : 5425 : cpl.u.blob_basic.cb_arg = cb_arg;
8508 : :
8509 : 5425 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
8510 [ - + ]: 5425 : if (!seq) {
8511 : 0 : cb_fn(cb_arg, -ENOMEM);
8512 : 0 : return;
8513 : : }
8514 : :
8515 : 5425 : spdk_bs_open_blob(bs, blobid, bs_delete_open_cpl, seq);
8516 : : }
8517 : :
8518 : : /* END spdk_bs_delete_blob */
8519 : :
8520 : : /* START spdk_bs_open_blob */
8521 : :
8522 : : static void
8523 : 16706 : bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8524 : : {
8525 : 16706 : struct spdk_blob *blob = cb_arg;
8526 : : struct spdk_blob *existing;
8527 : :
8528 [ + + ]: 16706 : if (bserrno != 0) {
8529 : 192 : blob_free(blob);
8530 : 192 : seq->cpl.u.blob_handle.blob = NULL;
8531 : 192 : bs_sequence_finish(seq, bserrno);
8532 : 192 : return;
8533 : : }
8534 : :
8535 : 16514 : existing = blob_lookup(blob->bs, blob->id);
8536 [ + + ]: 16514 : if (existing) {
8537 : 25 : blob_free(blob);
8538 : 25 : existing->open_ref++;
8539 : 25 : seq->cpl.u.blob_handle.blob = existing;
8540 : 25 : bs_sequence_finish(seq, 0);
8541 : 25 : return;
8542 : : }
8543 : :
8544 : 16489 : blob->open_ref++;
8545 : :
8546 : 16489 : spdk_bit_array_set(blob->bs->open_blobids, blob->id);
8547 : 16489 : RB_INSERT(spdk_blob_tree, &blob->bs->open_blobs, blob);
8548 : :
8549 : 16489 : bs_sequence_finish(seq, bserrno);
8550 : : }
8551 : :
8552 : : static inline void
8553 : 368 : blob_open_opts_copy(const struct spdk_blob_open_opts *src, struct spdk_blob_open_opts *dst)
8554 : : {
8555 : : #define FIELD_OK(field) \
8556 : : offsetof(struct spdk_blob_open_opts, field) + sizeof(src->field) <= src->opts_size
8557 : :
8558 : : #define SET_FIELD(field) \
8559 : : if (FIELD_OK(field)) { \
8560 : : dst->field = src->field; \
8561 : : } \
8562 : :
8563 [ + - ]: 368 : SET_FIELD(clear_method);
8564 [ + - ]: 368 : SET_FIELD(esnap_ctx);
8565 : :
8566 : 368 : dst->opts_size = src->opts_size;
8567 : :
8568 : : /* You should not remove this statement, but need to update the assert statement
8569 : : * if you add a new field, and also add a corresponding SET_FIELD statement */
8570 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_open_opts) == 24, "Incorrect size");
8571 : :
8572 : : #undef FIELD_OK
8573 : : #undef SET_FIELD
8574 : 368 : }
8575 : :
8576 : : static void
8577 : 19320 : bs_open_blob(struct spdk_blob_store *bs,
8578 : : spdk_blob_id blobid,
8579 : : struct spdk_blob_open_opts *opts,
8580 : : spdk_blob_op_with_handle_complete cb_fn,
8581 : : void *cb_arg)
8582 : : {
8583 : : struct spdk_blob *blob;
8584 : 18937 : struct spdk_bs_cpl cpl;
8585 : 18937 : struct spdk_blob_open_opts opts_local;
8586 : : spdk_bs_sequence_t *seq;
8587 : : uint32_t page_num;
8588 : :
8589 [ - + - + ]: 19320 : SPDK_DEBUGLOG(blob, "Opening blob 0x%" PRIx64 "\n", blobid);
8590 [ - + ]: 19320 : assert(spdk_get_thread() == bs->md_thread);
8591 : :
8592 : 19320 : page_num = bs_blobid_to_page(blobid);
8593 [ + + ]: 19320 : if (spdk_bit_array_get(bs->used_blobids, page_num) == false) {
8594 : : /* Invalid blobid */
8595 : 145 : cb_fn(cb_arg, NULL, -ENOENT);
8596 : 180 : return;
8597 : : }
8598 : :
8599 : 19175 : blob = blob_lookup(bs, blobid);
8600 [ + + ]: 19175 : if (blob) {
8601 : 2469 : blob->open_ref++;
8602 : 2469 : cb_fn(cb_arg, blob, 0);
8603 : 2469 : return;
8604 : : }
8605 : :
8606 : 16706 : blob = blob_alloc(bs, blobid);
8607 [ - + ]: 16706 : if (!blob) {
8608 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
8609 : 0 : return;
8610 : : }
8611 : :
8612 : 16706 : spdk_blob_open_opts_init(&opts_local, sizeof(opts_local));
8613 [ + + ]: 16706 : if (opts) {
8614 : 368 : blob_open_opts_copy(opts, &opts_local);
8615 : : }
8616 : :
8617 : 16706 : blob->clear_method = opts_local.clear_method;
8618 : :
8619 : 16706 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
8620 : 16706 : cpl.u.blob_handle.cb_fn = cb_fn;
8621 : 16706 : cpl.u.blob_handle.cb_arg = cb_arg;
8622 : 16706 : cpl.u.blob_handle.blob = blob;
8623 : 16706 : cpl.u.blob_handle.esnap_ctx = opts_local.esnap_ctx;
8624 : :
8625 : 16706 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
8626 [ - + ]: 16706 : if (!seq) {
8627 : 0 : blob_free(blob);
8628 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
8629 : 0 : return;
8630 : : }
8631 : :
8632 : 16706 : blob_load(seq, blob, bs_open_blob_cpl, blob);
8633 : : }
8634 : :
8635 : : void
8636 : 18916 : spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
8637 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
8638 : : {
8639 : 18916 : bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg);
8640 : 18916 : }
8641 : :
8642 : : void
8643 : 404 : spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid,
8644 : : struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
8645 : : {
8646 : 404 : bs_open_blob(bs, blobid, opts, cb_fn, cb_arg);
8647 : 404 : }
8648 : :
8649 : : /* END spdk_bs_open_blob */
8650 : :
8651 : : /* START spdk_blob_set_read_only */
8652 : : int
8653 : 736 : spdk_blob_set_read_only(struct spdk_blob *blob)
8654 : : {
8655 : 736 : blob_verify_md_op(blob);
8656 : :
8657 : 736 : blob->data_ro_flags |= SPDK_BLOB_READ_ONLY;
8658 : :
8659 : 736 : blob->state = SPDK_BLOB_STATE_DIRTY;
8660 : 736 : return 0;
8661 : : }
8662 : : /* END spdk_blob_set_read_only */
8663 : :
8664 : : /* START spdk_blob_sync_md */
8665 : :
8666 : : static void
8667 : 42937 : blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8668 : : {
8669 : 42937 : struct spdk_blob *blob = cb_arg;
8670 : :
8671 [ + - + + ]: 42937 : if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
8672 : 1257 : blob->data_ro = true;
8673 : 1257 : blob->md_ro = true;
8674 : : }
8675 : :
8676 : 42937 : bs_sequence_finish(seq, bserrno);
8677 : 42937 : }
8678 : :
8679 : : static void
8680 : 42937 : blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
8681 : : {
8682 : 42858 : struct spdk_bs_cpl cpl;
8683 : : spdk_bs_sequence_t *seq;
8684 : :
8685 : 42937 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
8686 : 42937 : cpl.u.blob_basic.cb_fn = cb_fn;
8687 : 42937 : cpl.u.blob_basic.cb_arg = cb_arg;
8688 : :
8689 : 42937 : seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
8690 [ - + ]: 42937 : if (!seq) {
8691 : 0 : cb_fn(cb_arg, -ENOMEM);
8692 : 0 : return;
8693 : : }
8694 : :
8695 : 42937 : blob_persist(seq, blob, blob_sync_md_cpl, blob);
8696 : : }
8697 : :
8698 : : void
8699 : 41345 : spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
8700 : : {
8701 : 41345 : blob_verify_md_op(blob);
8702 : :
8703 [ - + - + ]: 41345 : SPDK_DEBUGLOG(blob, "Syncing blob 0x%" PRIx64 "\n", blob->id);
8704 : :
8705 [ + + + + ]: 41345 : if (blob->md_ro) {
8706 [ - + ]: 12 : assert(blob->state == SPDK_BLOB_STATE_CLEAN);
8707 : 12 : cb_fn(cb_arg, 0);
8708 : 12 : return;
8709 : : }
8710 : :
8711 : 41333 : blob_sync_md(blob, cb_fn, cb_arg);
8712 : : }
8713 : :
8714 : : /* END spdk_blob_sync_md */
8715 : :
8716 : : struct spdk_blob_cluster_op_ctx {
8717 : : struct spdk_thread *thread;
8718 : : struct spdk_blob *blob;
8719 : : uint32_t cluster_num; /* cluster index in blob */
8720 : : uint32_t cluster; /* cluster on disk */
8721 : : uint32_t extent_page; /* extent page on disk */
8722 : : struct spdk_blob_md_page *page; /* preallocated extent page */
8723 : : int rc;
8724 : : spdk_blob_op_complete cb_fn;
8725 : : void *cb_arg;
8726 : : };
8727 : :
8728 : : static void
8729 : 4406 : blob_op_cluster_msg_cpl(void *arg)
8730 : : {
8731 : 4406 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8732 : :
8733 : 4406 : ctx->cb_fn(ctx->cb_arg, ctx->rc);
8734 : 4406 : free(ctx);
8735 : 4406 : }
8736 : :
8737 : : static void
8738 : 4308 : blob_op_cluster_msg_cb(void *arg, int bserrno)
8739 : : {
8740 : 4308 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8741 : :
8742 : 4308 : ctx->rc = bserrno;
8743 : 4308 : spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
8744 : 4308 : }
8745 : :
8746 : : static void
8747 : 308 : blob_insert_new_ep_cb(void *arg, int bserrno)
8748 : : {
8749 : 308 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8750 : : uint32_t *extent_page;
8751 : :
8752 : 308 : extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
8753 : 308 : *extent_page = ctx->extent_page;
8754 : 308 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8755 : 308 : blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
8756 : 308 : }
8757 : :
8758 : : struct spdk_blob_write_extent_page_ctx {
8759 : : struct spdk_blob_store *bs;
8760 : :
8761 : : uint32_t extent;
8762 : : struct spdk_blob_md_page *page;
8763 : : };
8764 : :
8765 : : static void
8766 : 78 : blob_free_cluster_msg_cb(void *arg, int bserrno)
8767 : : {
8768 : 78 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8769 : :
8770 : 78 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8771 : 78 : bs_release_cluster(ctx->blob->bs, ctx->cluster);
8772 : 78 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8773 : :
8774 : 78 : ctx->rc = bserrno;
8775 : 78 : spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
8776 : 78 : }
8777 : :
8778 : : static void
8779 : 78 : blob_free_cluster_update_ep_cb(void *arg, int bserrno)
8780 : : {
8781 : 78 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8782 : :
8783 [ + - + + : 78 : if (bserrno != 0 || ctx->blob->bs->clean == 0) {
+ - ]
8784 : 78 : blob_free_cluster_msg_cb(ctx, bserrno);
8785 : 78 : return;
8786 : : }
8787 : :
8788 : 0 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8789 : 0 : blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
8790 : : }
8791 : :
8792 : : static void
8793 : 0 : blob_free_cluster_free_ep_cb(void *arg, int bserrno)
8794 : : {
8795 : 0 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8796 : :
8797 : 0 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8798 [ # # ]: 0 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8799 : 0 : bs_release_md_page(ctx->blob->bs, ctx->extent_page);
8800 : 0 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8801 : 0 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8802 : 0 : blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
8803 : 0 : }
8804 : :
8805 : : static void
8806 : 3076 : blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8807 : : {
8808 : 3076 : struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
8809 : :
8810 : 3076 : free(ctx);
8811 : 3076 : bs_sequence_finish(seq, bserrno);
8812 : 3076 : }
8813 : :
8814 : : static void
8815 : 3076 : blob_write_extent_page_ready(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8816 : : {
8817 : 3076 : struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
8818 : :
8819 [ - + ]: 3076 : if (bserrno != 0) {
8820 : 0 : blob_persist_extent_page_cpl(seq, ctx, bserrno);
8821 : 0 : return;
8822 : : }
8823 : 3076 : bs_sequence_write_dev(seq, ctx->page, bs_md_page_to_lba(ctx->bs, ctx->extent),
8824 : 3076 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
8825 : : blob_persist_extent_page_cpl, ctx);
8826 : : }
8827 : :
8828 : : static void
8829 : 3076 : blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
8830 : : struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
8831 : : {
8832 : : struct spdk_blob_write_extent_page_ctx *ctx;
8833 : : spdk_bs_sequence_t *seq;
8834 : 2957 : struct spdk_bs_cpl cpl;
8835 : :
8836 : 3076 : ctx = calloc(1, sizeof(*ctx));
8837 [ - + ]: 3076 : if (!ctx) {
8838 : 0 : cb_fn(cb_arg, -ENOMEM);
8839 : 0 : return;
8840 : : }
8841 : 3076 : ctx->bs = blob->bs;
8842 : 3076 : ctx->extent = extent;
8843 : 3076 : ctx->page = page;
8844 : :
8845 : 3076 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
8846 : 3076 : cpl.u.blob_basic.cb_fn = cb_fn;
8847 : 3076 : cpl.u.blob_basic.cb_arg = cb_arg;
8848 : :
8849 : 3076 : seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
8850 [ - + ]: 3076 : if (!seq) {
8851 : 0 : free(ctx);
8852 : 0 : cb_fn(cb_arg, -ENOMEM);
8853 : 0 : return;
8854 : : }
8855 : :
8856 [ - + ]: 3076 : assert(page);
8857 : 3076 : page->next = SPDK_INVALID_MD_PAGE;
8858 : 3076 : page->id = blob->id;
8859 : 3076 : page->sequence_num = 0;
8860 : :
8861 : 3076 : blob_serialize_extent_page(blob, cluster_num, page);
8862 : :
8863 : 3076 : page->crc = blob_md_page_calc_crc(page);
8864 : :
8865 [ - + ]: 3076 : assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true);
8866 : :
8867 : 3076 : bs_mark_dirty(seq, blob->bs, blob_write_extent_page_ready, ctx);
8868 : : }
8869 : :
8870 : : static void
8871 : 4226 : blob_insert_cluster_msg(void *arg)
8872 : : {
8873 : 4226 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8874 : : uint32_t *extent_page;
8875 : :
8876 : 4226 : ctx->rc = blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster);
8877 [ + + ]: 4226 : if (ctx->rc != 0) {
8878 : 20 : spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
8879 : 20 : return;
8880 : : }
8881 : :
8882 [ + + + + ]: 4206 : if (ctx->blob->use_extent_table == false) {
8883 : : /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
8884 : 1218 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8885 : 1218 : blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
8886 : 1218 : return;
8887 : : }
8888 : :
8889 : 2988 : extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
8890 [ + + ]: 2988 : if (*extent_page == 0) {
8891 : : /* Extent page requires allocation.
8892 : : * It was already claimed in the used_md_pages map and placed in ctx. */
8893 [ - + ]: 308 : assert(ctx->extent_page != 0);
8894 [ - + ]: 308 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8895 : 308 : blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
8896 : : blob_insert_new_ep_cb, ctx);
8897 : : } else {
8898 : : /* It is possible for original thread to allocate extent page for
8899 : : * different cluster in the same extent page. In such case proceed with
8900 : : * updating the existing extent page, but release the additional one. */
8901 [ + + ]: 2680 : if (ctx->extent_page != 0) {
8902 : 3 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8903 [ - + ]: 3 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8904 : 3 : bs_release_md_page(ctx->blob->bs, ctx->extent_page);
8905 : 3 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8906 : 3 : ctx->extent_page = 0;
8907 : : }
8908 : : /* Extent page already allocated.
8909 : : * Every cluster allocation, requires just an update of single extent page. */
8910 : 2680 : blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
8911 : : blob_op_cluster_msg_cb, ctx);
8912 : : }
8913 : : }
8914 : :
8915 : : static void
8916 : 4226 : blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
8917 : : uint64_t cluster, uint32_t extent_page, struct spdk_blob_md_page *page,
8918 : : spdk_blob_op_complete cb_fn, void *cb_arg)
8919 : : {
8920 : : struct spdk_blob_cluster_op_ctx *ctx;
8921 : :
8922 : 4226 : ctx = calloc(1, sizeof(*ctx));
8923 [ - + ]: 4226 : if (ctx == NULL) {
8924 : 0 : cb_fn(cb_arg, -ENOMEM);
8925 : 0 : return;
8926 : : }
8927 : :
8928 : 4226 : ctx->thread = spdk_get_thread();
8929 : 4226 : ctx->blob = blob;
8930 : 4226 : ctx->cluster_num = cluster_num;
8931 : 4226 : ctx->cluster = cluster;
8932 : 4226 : ctx->extent_page = extent_page;
8933 : 4226 : ctx->page = page;
8934 : 4226 : ctx->cb_fn = cb_fn;
8935 : 4226 : ctx->cb_arg = cb_arg;
8936 : :
8937 : 4226 : spdk_thread_send_msg(blob->bs->md_thread, blob_insert_cluster_msg, ctx);
8938 : : }
8939 : :
8940 : : static void
8941 : 180 : blob_free_cluster_msg(void *arg)
8942 : : {
8943 : 180 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8944 : : uint32_t *extent_page;
8945 : : uint32_t start_cluster_idx;
8946 : 180 : bool free_extent_page = true;
8947 : : size_t i;
8948 : :
8949 : 180 : ctx->cluster = bs_lba_to_cluster(ctx->blob->bs, ctx->blob->active.clusters[ctx->cluster_num]);
8950 : :
8951 : : /* There were concurrent unmaps to the same cluster, only release the cluster on the first one */
8952 [ + + ]: 180 : if (ctx->cluster == 0) {
8953 : 24 : blob_op_cluster_msg_cb(ctx, 0);
8954 : 24 : return;
8955 : : }
8956 : :
8957 : 156 : ctx->blob->active.clusters[ctx->cluster_num] = 0;
8958 [ + - ]: 156 : if (ctx->cluster != 0) {
8959 : 156 : ctx->blob->active.num_allocated_clusters--;
8960 : : }
8961 : :
8962 [ + + + + ]: 156 : if (ctx->blob->use_extent_table == false) {
8963 : : /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
8964 : 78 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8965 : 78 : bs_release_cluster(ctx->blob->bs, ctx->cluster);
8966 : 78 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8967 : 78 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8968 : 78 : blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
8969 : 78 : return;
8970 : : }
8971 : :
8972 : 78 : extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
8973 : :
8974 : : /* There shouldn't be parallel release operations on same cluster */
8975 [ - + ]: 78 : assert(*extent_page == ctx->extent_page);
8976 : :
8977 [ - + ]: 78 : start_cluster_idx = (ctx->cluster_num / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
8978 [ + - ]: 144 : for (i = 0; i < SPDK_EXTENTS_PER_EP; ++i) {
8979 [ + + ]: 144 : if (ctx->blob->active.clusters[start_cluster_idx + i] != 0) {
8980 : 78 : free_extent_page = false;
8981 : 78 : break;
8982 : : }
8983 : : }
8984 : :
8985 [ - + ]: 78 : if (free_extent_page) {
8986 [ # # ]: 0 : assert(ctx->extent_page != 0);
8987 [ # # ]: 0 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8988 : 0 : ctx->blob->active.extent_pages[bs_cluster_to_extent_table_id(ctx->cluster_num)] = 0;
8989 : 0 : blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
8990 : : blob_free_cluster_free_ep_cb, ctx);
8991 : : } else {
8992 : 78 : blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
8993 : : blob_free_cluster_update_ep_cb, ctx);
8994 : : }
8995 : : }
8996 : :
8997 : :
8998 : : static void
8999 : 180 : blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, uint32_t extent_page,
9000 : : struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
9001 : : {
9002 : : struct spdk_blob_cluster_op_ctx *ctx;
9003 : :
9004 : 180 : ctx = calloc(1, sizeof(*ctx));
9005 [ - + ]: 180 : if (ctx == NULL) {
9006 : 0 : cb_fn(cb_arg, -ENOMEM);
9007 : 0 : return;
9008 : : }
9009 : :
9010 : 180 : ctx->thread = spdk_get_thread();
9011 : 180 : ctx->blob = blob;
9012 : 180 : ctx->cluster_num = cluster_num;
9013 : 180 : ctx->extent_page = extent_page;
9014 : 180 : ctx->page = page;
9015 : 180 : ctx->cb_fn = cb_fn;
9016 : 180 : ctx->cb_arg = cb_arg;
9017 : :
9018 : 180 : spdk_thread_send_msg(blob->bs->md_thread, blob_free_cluster_msg, ctx);
9019 : : }
9020 : :
9021 : : /* START spdk_blob_close */
9022 : :
9023 : : static void
9024 : 18983 : blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9025 : : {
9026 : 18983 : struct spdk_blob *blob = cb_arg;
9027 : :
9028 [ + - ]: 18983 : if (bserrno == 0) {
9029 : 18983 : blob->open_ref--;
9030 [ + + ]: 18983 : if (blob->open_ref == 0) {
9031 : : /*
9032 : : * Blobs with active.num_pages == 0 are deleted blobs.
9033 : : * these blobs are removed from the blob_store list
9034 : : * when the deletion process starts - so don't try to
9035 : : * remove them again.
9036 : : */
9037 [ + + ]: 16489 : if (blob->active.num_pages > 0) {
9038 : 11280 : spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
9039 : 11280 : RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
9040 : : }
9041 : 16489 : blob_free(blob);
9042 : : }
9043 : : }
9044 : :
9045 : 18983 : bs_sequence_finish(seq, bserrno);
9046 : 18983 : }
9047 : :
9048 : : static void
9049 : 414 : blob_close_esnap_done(void *cb_arg, struct spdk_blob *blob, int bserrno)
9050 : : {
9051 : 414 : spdk_bs_sequence_t *seq = cb_arg;
9052 : :
9053 [ - + ]: 414 : if (bserrno != 0) {
9054 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": close failed with error %d\n",
9055 : : blob->id, bserrno);
9056 : 0 : bs_sequence_finish(seq, bserrno);
9057 : 0 : return;
9058 : : }
9059 : :
9060 [ - + - + ]: 414 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": closed, syncing metadata on thread %s\n",
9061 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
9062 : :
9063 : : /* Sync metadata */
9064 : 414 : blob_persist(seq, blob, blob_close_cpl, blob);
9065 : : }
9066 : :
9067 : : void
9068 : 18983 : spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
9069 : : {
9070 : 18600 : struct spdk_bs_cpl cpl;
9071 : : spdk_bs_sequence_t *seq;
9072 : :
9073 : 18983 : blob_verify_md_op(blob);
9074 : :
9075 [ - + - + ]: 18983 : SPDK_DEBUGLOG(blob, "Closing blob 0x%" PRIx64 "\n", blob->id);
9076 : :
9077 [ - + ]: 18983 : if (blob->open_ref == 0) {
9078 : 0 : cb_fn(cb_arg, -EBADF);
9079 : 0 : return;
9080 : : }
9081 : :
9082 : 18983 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
9083 : 18983 : cpl.u.blob_basic.cb_fn = cb_fn;
9084 : 18983 : cpl.u.blob_basic.cb_arg = cb_arg;
9085 : :
9086 : 18983 : seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
9087 [ - + ]: 18983 : if (!seq) {
9088 : 0 : cb_fn(cb_arg, -ENOMEM);
9089 : 0 : return;
9090 : : }
9091 : :
9092 [ + + + + ]: 18983 : if (blob->open_ref == 1 && blob_is_esnap_clone(blob)) {
9093 : 414 : blob_esnap_destroy_bs_dev_channels(blob, false, blob_close_esnap_done, seq);
9094 : 414 : return;
9095 : : }
9096 : :
9097 : : /* Sync metadata */
9098 : 18569 : blob_persist(seq, blob, blob_close_cpl, blob);
9099 : : }
9100 : :
9101 : : /* END spdk_blob_close */
9102 : :
9103 : 1364 : struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
9104 : : {
9105 : 1364 : return spdk_get_io_channel(bs);
9106 : : }
9107 : :
9108 : : void
9109 : 732 : spdk_bs_free_io_channel(struct spdk_io_channel *channel)
9110 : : {
9111 : 732 : blob_esnap_destroy_bs_channel(spdk_io_channel_get_ctx(channel));
9112 : 732 : spdk_put_io_channel(channel);
9113 : 732 : }
9114 : :
9115 : : void
9116 : 144784 : spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
9117 : : uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
9118 : : {
9119 : 144784 : blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
9120 : : SPDK_BLOB_UNMAP);
9121 : 144784 : }
9122 : :
9123 : : void
9124 : 149 : spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
9125 : : uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
9126 : : {
9127 : 149 : blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
9128 : : SPDK_BLOB_WRITE_ZEROES);
9129 : 149 : }
9130 : :
9131 : : void
9132 : 300164 : spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
9133 : : void *payload, uint64_t offset, uint64_t length,
9134 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9135 : : {
9136 : 300164 : blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
9137 : : SPDK_BLOB_WRITE);
9138 : 300164 : }
9139 : :
9140 : : void
9141 : 1404594 : spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
9142 : : void *payload, uint64_t offset, uint64_t length,
9143 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9144 : : {
9145 : 1404594 : blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
9146 : : SPDK_BLOB_READ);
9147 : 1404594 : }
9148 : :
9149 : : void
9150 : 420 : spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
9151 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9152 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9153 : : {
9154 : 420 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false, NULL);
9155 : 420 : }
9156 : :
9157 : : void
9158 : 2820 : spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
9159 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9160 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9161 : : {
9162 : 2820 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true, NULL);
9163 : 2820 : }
9164 : :
9165 : : void
9166 : 2771185 : spdk_blob_io_writev_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
9167 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9168 : : spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
9169 : : {
9170 : 2771185 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false,
9171 : : io_opts);
9172 : 2771185 : }
9173 : :
9174 : : void
9175 : 2812452 : spdk_blob_io_readv_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
9176 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9177 : : spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
9178 : : {
9179 : 2812452 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true,
9180 : : io_opts);
9181 : 2812452 : }
9182 : :
9183 : : struct spdk_bs_iter_ctx {
9184 : : int64_t page_num;
9185 : : struct spdk_blob_store *bs;
9186 : :
9187 : : spdk_blob_op_with_handle_complete cb_fn;
9188 : : void *cb_arg;
9189 : : };
9190 : :
9191 : : static void
9192 : 7275 : bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
9193 : : {
9194 : 7275 : struct spdk_bs_iter_ctx *ctx = cb_arg;
9195 : 7275 : struct spdk_blob_store *bs = ctx->bs;
9196 : : spdk_blob_id id;
9197 : :
9198 [ + + ]: 7275 : if (bserrno == 0) {
9199 : 3150 : ctx->cb_fn(ctx->cb_arg, _blob, bserrno);
9200 : 3150 : free(ctx);
9201 : 3150 : return;
9202 : : }
9203 : :
9204 : 4125 : ctx->page_num++;
9205 : 4125 : ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num);
9206 [ + + ]: 4125 : if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) {
9207 : 951 : ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
9208 : 951 : free(ctx);
9209 : 951 : return;
9210 : : }
9211 : :
9212 : 3174 : id = bs_page_to_blobid(ctx->page_num);
9213 : :
9214 : 3174 : spdk_bs_open_blob(bs, id, bs_iter_cpl, ctx);
9215 : : }
9216 : :
9217 : : void
9218 : 1023 : spdk_bs_iter_first(struct spdk_blob_store *bs,
9219 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
9220 : : {
9221 : : struct spdk_bs_iter_ctx *ctx;
9222 : :
9223 : 1023 : ctx = calloc(1, sizeof(*ctx));
9224 [ - + ]: 1023 : if (!ctx) {
9225 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
9226 : 0 : return;
9227 : : }
9228 : :
9229 : 1023 : ctx->page_num = -1;
9230 : 1023 : ctx->bs = bs;
9231 : 1023 : ctx->cb_fn = cb_fn;
9232 : 1023 : ctx->cb_arg = cb_arg;
9233 : :
9234 : 1023 : bs_iter_cpl(ctx, NULL, -1);
9235 : : }
9236 : :
9237 : : static void
9238 : 3078 : bs_iter_close_cpl(void *cb_arg, int bserrno)
9239 : : {
9240 : 3078 : struct spdk_bs_iter_ctx *ctx = cb_arg;
9241 : :
9242 : 3078 : bs_iter_cpl(ctx, NULL, -1);
9243 : 3078 : }
9244 : :
9245 : : void
9246 : 3078 : spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob,
9247 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
9248 : : {
9249 : : struct spdk_bs_iter_ctx *ctx;
9250 : :
9251 [ - + ]: 3078 : assert(blob != NULL);
9252 : :
9253 : 3078 : ctx = calloc(1, sizeof(*ctx));
9254 [ - + ]: 3078 : if (!ctx) {
9255 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
9256 : 0 : return;
9257 : : }
9258 : :
9259 : 3078 : ctx->page_num = bs_blobid_to_page(blob->id);
9260 : 3078 : ctx->bs = bs;
9261 : 3078 : ctx->cb_fn = cb_fn;
9262 : 3078 : ctx->cb_arg = cb_arg;
9263 : :
9264 : : /* Close the existing blob */
9265 : 3078 : spdk_blob_close(blob, bs_iter_close_cpl, ctx);
9266 : : }
9267 : :
9268 : : static int
9269 : 17654 : blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
9270 : : uint16_t value_len, bool internal)
9271 : : {
9272 : : struct spdk_xattr_tailq *xattrs;
9273 : : struct spdk_xattr *xattr;
9274 : : size_t desc_size;
9275 : : void *tmp;
9276 : :
9277 : 17654 : blob_verify_md_op(blob);
9278 : :
9279 [ + + + + ]: 17654 : if (blob->md_ro) {
9280 : 12 : return -EPERM;
9281 : : }
9282 : :
9283 [ - + ]: 17642 : desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len;
9284 [ + + ]: 17642 : if (desc_size > SPDK_BS_MAX_DESC_SIZE) {
9285 [ - + - + ]: 12 : SPDK_DEBUGLOG(blob, "Xattr '%s' of size %zu does not fix into single page %zu\n", name,
9286 : : desc_size, SPDK_BS_MAX_DESC_SIZE);
9287 : 12 : return -ENOMEM;
9288 : : }
9289 : :
9290 [ + + ]: 17630 : if (internal) {
9291 : 2304 : xattrs = &blob->xattrs_internal;
9292 : 2304 : blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR;
9293 : : } else {
9294 : 15326 : xattrs = &blob->xattrs;
9295 : : }
9296 : :
9297 [ + + ]: 31814 : TAILQ_FOREACH(xattr, xattrs, link) {
9298 [ + + - + : 26853 : if (!strcmp(name, xattr->name)) {
+ + ]
9299 : 12669 : tmp = malloc(value_len);
9300 [ - + ]: 12669 : if (!tmp) {
9301 : 0 : return -ENOMEM;
9302 : : }
9303 : :
9304 : 12669 : free(xattr->value);
9305 : 12669 : xattr->value_len = value_len;
9306 : 12669 : xattr->value = tmp;
9307 [ - + - + ]: 12669 : memcpy(xattr->value, value, value_len);
9308 : :
9309 : 12669 : blob->state = SPDK_BLOB_STATE_DIRTY;
9310 : :
9311 : 12669 : return 0;
9312 : : }
9313 : : }
9314 : :
9315 : 4961 : xattr = calloc(1, sizeof(*xattr));
9316 [ - + ]: 4961 : if (!xattr) {
9317 : 0 : return -ENOMEM;
9318 : : }
9319 : :
9320 [ - + ]: 4961 : xattr->name = strdup(name);
9321 [ - + ]: 4961 : if (!xattr->name) {
9322 : 0 : free(xattr);
9323 : 0 : return -ENOMEM;
9324 : : }
9325 : :
9326 : 4961 : xattr->value_len = value_len;
9327 : 4961 : xattr->value = malloc(value_len);
9328 [ - + ]: 4961 : if (!xattr->value) {
9329 : 0 : free(xattr->name);
9330 : 0 : free(xattr);
9331 : 0 : return -ENOMEM;
9332 : : }
9333 [ - + - + ]: 4961 : memcpy(xattr->value, value, value_len);
9334 : 4961 : TAILQ_INSERT_TAIL(xattrs, xattr, link);
9335 : :
9336 : 4961 : blob->state = SPDK_BLOB_STATE_DIRTY;
9337 : :
9338 : 4961 : return 0;
9339 : : }
9340 : :
9341 : : int
9342 : 14628 : spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
9343 : : uint16_t value_len)
9344 : : {
9345 : 14628 : return blob_set_xattr(blob, name, value, value_len, false);
9346 : : }
9347 : :
9348 : : static int
9349 : 1268 : blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal)
9350 : : {
9351 : : struct spdk_xattr_tailq *xattrs;
9352 : : struct spdk_xattr *xattr;
9353 : :
9354 : 1268 : blob_verify_md_op(blob);
9355 : :
9356 [ + + + + ]: 1268 : if (blob->md_ro) {
9357 : 12 : return -EPERM;
9358 : : }
9359 [ + + ]: 1256 : xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
9360 : :
9361 [ + + ]: 1293 : TAILQ_FOREACH(xattr, xattrs, link) {
9362 [ + + - + : 1132 : if (!strcmp(name, xattr->name)) {
+ + ]
9363 [ + + ]: 1095 : TAILQ_REMOVE(xattrs, xattr, link);
9364 : 1095 : free(xattr->value);
9365 : 1095 : free(xattr->name);
9366 : 1095 : free(xattr);
9367 : :
9368 [ + + + + ]: 1095 : if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) {
9369 : 756 : blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR;
9370 : : }
9371 : 1095 : blob->state = SPDK_BLOB_STATE_DIRTY;
9372 : :
9373 : 1095 : return 0;
9374 : : }
9375 : : }
9376 : :
9377 : 161 : return -ENOENT;
9378 : : }
9379 : :
9380 : : int
9381 : 109 : spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name)
9382 : : {
9383 : 109 : return blob_remove_xattr(blob, name, false);
9384 : : }
9385 : :
9386 : : static int
9387 : 14391 : blob_get_xattr_value(struct spdk_blob *blob, const char *name,
9388 : : const void **value, size_t *value_len, bool internal)
9389 : : {
9390 : : struct spdk_xattr *xattr;
9391 : : struct spdk_xattr_tailq *xattrs;
9392 : :
9393 [ + + ]: 14391 : xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
9394 : :
9395 [ + + ]: 20007 : TAILQ_FOREACH(xattr, xattrs, link) {
9396 [ + + - + : 10747 : if (!strcmp(name, xattr->name)) {
+ + ]
9397 : 5131 : *value = xattr->value;
9398 : 5131 : *value_len = xattr->value_len;
9399 : 5131 : return 0;
9400 : : }
9401 : : }
9402 : 9260 : return -ENOENT;
9403 : : }
9404 : :
9405 : : int
9406 : 4258 : spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
9407 : : const void **value, size_t *value_len)
9408 : : {
9409 : 4258 : blob_verify_md_op(blob);
9410 : :
9411 : 4258 : return blob_get_xattr_value(blob, name, value, value_len, false);
9412 : : }
9413 : :
9414 : : struct spdk_xattr_names {
9415 : : uint32_t count;
9416 : : const char *names[0];
9417 : : };
9418 : :
9419 : : static int
9420 : 13 : blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names)
9421 : : {
9422 : : struct spdk_xattr *xattr;
9423 : 13 : int count = 0;
9424 : :
9425 [ + + ]: 38 : TAILQ_FOREACH(xattr, xattrs, link) {
9426 : 25 : count++;
9427 : : }
9428 : :
9429 : 13 : *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
9430 [ - + ]: 13 : if (*names == NULL) {
9431 : 0 : return -ENOMEM;
9432 : : }
9433 : :
9434 [ + + ]: 38 : TAILQ_FOREACH(xattr, xattrs, link) {
9435 : 25 : (*names)->names[(*names)->count++] = xattr->name;
9436 : : }
9437 : :
9438 : 13 : return 0;
9439 : : }
9440 : :
9441 : : int
9442 : 13 : spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names)
9443 : : {
9444 : 13 : blob_verify_md_op(blob);
9445 : :
9446 : 13 : return blob_get_xattr_names(&blob->xattrs, names);
9447 : : }
9448 : :
9449 : : uint32_t
9450 : 15 : spdk_xattr_names_get_count(struct spdk_xattr_names *names)
9451 : : {
9452 [ - + ]: 15 : assert(names != NULL);
9453 : :
9454 : 15 : return names->count;
9455 : : }
9456 : :
9457 : : const char *
9458 : 26 : spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
9459 : : {
9460 [ - + ]: 26 : if (index >= names->count) {
9461 : 0 : return NULL;
9462 : : }
9463 : :
9464 : 26 : return names->names[index];
9465 : : }
9466 : :
9467 : : void
9468 : 13 : spdk_xattr_names_free(struct spdk_xattr_names *names)
9469 : : {
9470 : 13 : free(names);
9471 : 13 : }
9472 : :
9473 : : struct spdk_bs_type
9474 : 17 : spdk_bs_get_bstype(struct spdk_blob_store *bs)
9475 : : {
9476 : 17 : return bs->bstype;
9477 : : }
9478 : :
9479 : : void
9480 : 0 : spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
9481 : : {
9482 : 0 : memcpy(&bs->bstype, &bstype, sizeof(bstype));
9483 : 0 : }
9484 : :
9485 : : bool
9486 : 6218 : spdk_blob_is_read_only(struct spdk_blob *blob)
9487 : : {
9488 [ - + ]: 6218 : assert(blob != NULL);
9489 [ + + + + : 6218 : return (blob->data_ro || blob->md_ro);
- + - + ]
9490 : : }
9491 : :
9492 : : bool
9493 : 2549 : spdk_blob_is_snapshot(struct spdk_blob *blob)
9494 : : {
9495 : : struct spdk_blob_list *snapshot_entry;
9496 : :
9497 [ - + ]: 2549 : assert(blob != NULL);
9498 : :
9499 : 2549 : snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
9500 [ + + ]: 2549 : if (snapshot_entry == NULL) {
9501 : 2398 : return false;
9502 : : }
9503 : :
9504 : 151 : return true;
9505 : : }
9506 : :
9507 : : bool
9508 : 2597 : spdk_blob_is_clone(struct spdk_blob *blob)
9509 : : {
9510 [ - + ]: 2597 : assert(blob != NULL);
9511 : :
9512 [ + + ]: 2597 : if (blob->parent_id != SPDK_BLOBID_INVALID &&
9513 [ + + ]: 292 : blob->parent_id != SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
9514 [ - + ]: 224 : assert(spdk_blob_is_thin_provisioned(blob));
9515 : 224 : return true;
9516 : : }
9517 : :
9518 : 2373 : return false;
9519 : : }
9520 : :
9521 : : bool
9522 : 1709568 : spdk_blob_is_thin_provisioned(struct spdk_blob *blob)
9523 : : {
9524 [ - + ]: 1709568 : assert(blob != NULL);
9525 : 1709568 : return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
9526 : : }
9527 : :
9528 : : bool
9529 : 7430851 : spdk_blob_is_esnap_clone(const struct spdk_blob *blob)
9530 : : {
9531 : 7430851 : return blob_is_esnap_clone(blob);
9532 : : }
9533 : :
9534 : : static void
9535 : 16586 : blob_update_clear_method(struct spdk_blob *blob)
9536 : : {
9537 : : enum blob_clear_method stored_cm;
9538 : :
9539 [ - + ]: 16586 : assert(blob != NULL);
9540 : :
9541 : : /* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored
9542 : : * in metadata previously. If something other than the default was
9543 : : * specified, ignore stored value and used what was passed in.
9544 : : */
9545 : 16586 : stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT);
9546 : :
9547 [ + + ]: 16586 : if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) {
9548 : 16583 : blob->clear_method = stored_cm;
9549 [ - + ]: 3 : } else if (blob->clear_method != stored_cm) {
9550 : 0 : SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n",
9551 : : blob->clear_method, stored_cm);
9552 : : }
9553 : 16586 : }
9554 : :
9555 : : spdk_blob_id
9556 : 828 : spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id)
9557 : : {
9558 : 828 : struct spdk_blob_list *snapshot_entry = NULL;
9559 : 828 : struct spdk_blob_list *clone_entry = NULL;
9560 : :
9561 [ + + ]: 1542 : TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
9562 [ + + ]: 2279 : TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
9563 [ + + ]: 1565 : if (clone_entry->id == blob_id) {
9564 : 555 : return snapshot_entry->id;
9565 : : }
9566 : : }
9567 : : }
9568 : :
9569 : 273 : return SPDK_BLOBID_INVALID;
9570 : : }
9571 : :
9572 : : int
9573 : 1227 : spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids,
9574 : : size_t *count)
9575 : : {
9576 : : struct spdk_blob_list *snapshot_entry, *clone_entry;
9577 : : size_t n;
9578 : :
9579 : 1227 : snapshot_entry = bs_get_snapshot_entry(bs, blobid);
9580 [ + + ]: 1227 : if (snapshot_entry == NULL) {
9581 : 574 : *count = 0;
9582 : 574 : return 0;
9583 : : }
9584 : :
9585 [ + + + + ]: 653 : if (ids == NULL || *count < snapshot_entry->clone_count) {
9586 : 106 : *count = snapshot_entry->clone_count;
9587 : 106 : return -ENOMEM;
9588 : : }
9589 : 547 : *count = snapshot_entry->clone_count;
9590 : :
9591 : 547 : n = 0;
9592 [ + + ]: 1137 : TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
9593 : 590 : ids[n++] = clone_entry->id;
9594 : : }
9595 : :
9596 : 547 : return 0;
9597 : : }
9598 : :
9599 : : static void
9600 : 12 : bs_load_grow_continue(struct spdk_bs_load_ctx *ctx)
9601 : : {
9602 : : int rc;
9603 : :
9604 [ - + ]: 12 : if (ctx->super->size == 0) {
9605 : 0 : ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9606 : : }
9607 : :
9608 [ - + ]: 12 : if (ctx->super->io_unit_size == 0) {
9609 : 0 : ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
9610 : : }
9611 : :
9612 : : /* Parse the super block */
9613 : 12 : ctx->bs->clean = 1;
9614 : 12 : ctx->bs->cluster_sz = ctx->super->cluster_size;
9615 [ - + ]: 12 : ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
9616 : 12 : ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
9617 [ + - ]: 12 : if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
9618 : 12 : ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
9619 : : }
9620 : 12 : ctx->bs->io_unit_size = ctx->super->io_unit_size;
9621 : 12 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
9622 [ - + ]: 12 : if (rc < 0) {
9623 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
9624 : 0 : return;
9625 : : }
9626 : 12 : ctx->bs->md_start = ctx->super->md_start;
9627 : 12 : ctx->bs->md_len = ctx->super->md_len;
9628 : 12 : rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
9629 [ - + ]: 12 : if (rc < 0) {
9630 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
9631 : 0 : return;
9632 : : }
9633 : :
9634 : 32 : ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
9635 : 20 : ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
9636 : 12 : ctx->bs->super_blob = ctx->super->super_blob;
9637 : 12 : memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
9638 : :
9639 [ + - - + ]: 12 : if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) {
9640 : 0 : SPDK_ERRLOG("Can not grow an unclean blobstore, please load it normally to clean it.\n");
9641 : 0 : bs_load_ctx_fail(ctx, -EIO);
9642 : 0 : return;
9643 : : } else {
9644 : 12 : bs_load_read_used_pages(ctx);
9645 : : }
9646 : : }
9647 : :
9648 : : static void
9649 : 12 : bs_load_grow_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9650 : : {
9651 : 12 : struct spdk_bs_load_ctx *ctx = cb_arg;
9652 : :
9653 [ - + ]: 12 : if (bserrno != 0) {
9654 : 0 : bs_load_ctx_fail(ctx, bserrno);
9655 : 0 : return;
9656 : : }
9657 : 12 : bs_load_grow_continue(ctx);
9658 : : }
9659 : :
9660 : : static void
9661 : 12 : bs_load_grow_used_clusters_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9662 : : {
9663 : 12 : struct spdk_bs_load_ctx *ctx = cb_arg;
9664 : :
9665 [ - + ]: 12 : if (bserrno != 0) {
9666 : 0 : bs_load_ctx_fail(ctx, bserrno);
9667 : 0 : return;
9668 : : }
9669 : :
9670 : 12 : spdk_free(ctx->mask);
9671 : :
9672 : 12 : bs_sequence_write_dev(ctx->seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
9673 : 12 : bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
9674 : : bs_load_grow_super_write_cpl, ctx);
9675 : : }
9676 : :
9677 : : static void
9678 : 12 : bs_load_grow_used_clusters_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9679 : : {
9680 : 12 : struct spdk_bs_load_ctx *ctx = cb_arg;
9681 : : uint64_t lba, lba_count;
9682 : : uint64_t dev_size;
9683 : : uint64_t total_clusters;
9684 : :
9685 [ - + ]: 12 : if (bserrno != 0) {
9686 : 0 : bs_load_ctx_fail(ctx, bserrno);
9687 : 0 : return;
9688 : : }
9689 : :
9690 : : /* The type must be correct */
9691 [ - + ]: 12 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
9692 : : /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
9693 [ - + ]: 12 : assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
9694 : : struct spdk_blob_md_page) * 8));
9695 : 12 : dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9696 [ - + ]: 12 : total_clusters = dev_size / ctx->super->cluster_size;
9697 : 12 : ctx->mask->length = total_clusters;
9698 : :
9699 : 12 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
9700 : 12 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
9701 : 12 : bs_sequence_write_dev(ctx->seq, ctx->mask, lba, lba_count,
9702 : : bs_load_grow_used_clusters_write_cpl, ctx);
9703 : : }
9704 : :
9705 : : static void
9706 : 12 : bs_load_try_to_grow(struct spdk_bs_load_ctx *ctx)
9707 : : {
9708 : : uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
9709 : : uint64_t lba, lba_count, mask_size;
9710 : :
9711 : 12 : dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9712 [ - + ]: 12 : total_clusters = dev_size / ctx->super->cluster_size;
9713 : 12 : used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
9714 : 12 : spdk_divide_round_up(total_clusters, 8),
9715 : : SPDK_BS_PAGE_SIZE);
9716 : 12 : max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
9717 : : /* No necessary to grow or no space to grow */
9718 [ + - - + ]: 12 : if (ctx->super->size >= dev_size || used_cluster_mask_len > max_used_cluster_mask) {
9719 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "No grow\n");
9720 : 0 : bs_load_grow_continue(ctx);
9721 : 0 : return;
9722 : : }
9723 : :
9724 [ - + - + ]: 12 : SPDK_DEBUGLOG(blob, "Resize blobstore\n");
9725 : :
9726 : 12 : ctx->super->size = dev_size;
9727 : 12 : ctx->super->used_cluster_mask_len = used_cluster_mask_len;
9728 : 12 : ctx->super->crc = blob_md_page_calc_crc(ctx->super);
9729 : :
9730 : 12 : mask_size = used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
9731 : 12 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
9732 : : SPDK_MALLOC_DMA);
9733 [ - + ]: 12 : if (!ctx->mask) {
9734 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
9735 : 0 : return;
9736 : : }
9737 : 12 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
9738 : 12 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
9739 : 12 : bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
9740 : : bs_load_grow_used_clusters_read_cpl, ctx);
9741 : : }
9742 : :
9743 : : static void
9744 : 12 : bs_grow_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9745 : : {
9746 : 12 : struct spdk_bs_load_ctx *ctx = cb_arg;
9747 : : int rc;
9748 : :
9749 : 12 : rc = bs_super_validate(ctx->super, ctx->bs);
9750 [ - + ]: 12 : if (rc != 0) {
9751 : 0 : bs_load_ctx_fail(ctx, rc);
9752 : 0 : return;
9753 : : }
9754 : :
9755 : 12 : bs_load_try_to_grow(ctx);
9756 : : }
9757 : :
9758 : : struct spdk_bs_grow_ctx {
9759 : : struct spdk_blob_store *bs;
9760 : : struct spdk_bs_super_block *super;
9761 : :
9762 : : struct spdk_bit_pool *new_used_clusters;
9763 : : struct spdk_bs_md_mask *new_used_clusters_mask;
9764 : :
9765 : : spdk_bs_sequence_t *seq;
9766 : : };
9767 : :
9768 : : static void
9769 : 98 : bs_grow_live_done(struct spdk_bs_grow_ctx *ctx, int bserrno)
9770 : : {
9771 [ + + ]: 98 : if (bserrno != 0) {
9772 : 24 : spdk_bit_pool_free(&ctx->new_used_clusters);
9773 : : }
9774 : :
9775 : 98 : bs_sequence_finish(ctx->seq, bserrno);
9776 : 98 : free(ctx->new_used_clusters_mask);
9777 : 98 : spdk_free(ctx->super);
9778 : 98 : free(ctx);
9779 : 98 : }
9780 : :
9781 : : static void
9782 : 26 : bs_grow_live_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9783 : : {
9784 : 26 : struct spdk_bs_grow_ctx *ctx = cb_arg;
9785 : 26 : struct spdk_blob_store *bs = ctx->bs;
9786 : : uint64_t total_clusters;
9787 : :
9788 [ - + ]: 26 : if (bserrno != 0) {
9789 : 0 : bs_grow_live_done(ctx, bserrno);
9790 : 0 : return;
9791 : : }
9792 : :
9793 : : /*
9794 : : * Blobstore is not clean until unload, for now only the super block is up to date.
9795 : : * This is similar to state right after blobstore init, when bs_write_used_md() didn't
9796 : : * yet execute.
9797 : : * When cleanly unloaded, the used md pages will be written out.
9798 : : * In case of unclean shutdown, loading blobstore will go through recovery path correctly
9799 : : * filling out the used_clusters with new size and writing it out.
9800 : : */
9801 : 26 : bs->clean = 0;
9802 : :
9803 : : /* Reverting the super->size past this point is complex, avoid any error paths
9804 : : * that require to do so. */
9805 : 26 : spdk_spin_lock(&bs->used_lock);
9806 : :
9807 [ - + ]: 26 : total_clusters = ctx->super->size / ctx->super->cluster_size;
9808 : :
9809 [ - + ]: 26 : assert(total_clusters >= spdk_bit_pool_capacity(bs->used_clusters));
9810 : 26 : spdk_bit_pool_store_mask(bs->used_clusters, ctx->new_used_clusters_mask);
9811 : :
9812 [ - + ]: 26 : assert(total_clusters == spdk_bit_pool_capacity(ctx->new_used_clusters));
9813 : 26 : spdk_bit_pool_load_mask(ctx->new_used_clusters, ctx->new_used_clusters_mask);
9814 : :
9815 : 26 : spdk_bit_pool_free(&bs->used_clusters);
9816 : 26 : bs->used_clusters = ctx->new_used_clusters;
9817 : :
9818 : 26 : bs->total_clusters = total_clusters;
9819 : 52 : bs->total_data_clusters = bs->total_clusters - spdk_divide_round_up(
9820 : 26 : bs->md_start + bs->md_len, bs->pages_per_cluster);
9821 : :
9822 : 26 : bs->num_free_clusters = spdk_bit_pool_count_free(bs->used_clusters);
9823 [ - + ]: 26 : assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
9824 : 26 : spdk_spin_unlock(&bs->used_lock);
9825 : :
9826 : 26 : bs_grow_live_done(ctx, 0);
9827 : : }
9828 : :
9829 : : static void
9830 : 98 : bs_grow_live_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9831 : : {
9832 : 98 : struct spdk_bs_grow_ctx *ctx = cb_arg;
9833 : : uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
9834 : : int rc;
9835 : :
9836 [ - + ]: 98 : if (bserrno != 0) {
9837 : 0 : bs_grow_live_done(ctx, bserrno);
9838 : 0 : return;
9839 : : }
9840 : :
9841 : 98 : rc = bs_super_validate(ctx->super, ctx->bs);
9842 [ + + ]: 98 : if (rc != 0) {
9843 : 12 : bs_grow_live_done(ctx, rc);
9844 : 12 : return;
9845 : : }
9846 : :
9847 : 86 : dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9848 [ - + ]: 86 : total_clusters = dev_size / ctx->super->cluster_size;
9849 : 86 : used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
9850 : 86 : spdk_divide_round_up(total_clusters, 8),
9851 : : SPDK_BS_PAGE_SIZE);
9852 : 86 : max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
9853 : : /* Only checking dev_size. Since it can change, but total_clusters remain the same. */
9854 [ + + ]: 86 : if (dev_size == ctx->super->size) {
9855 [ - + - + ]: 48 : SPDK_DEBUGLOG(blob, "No need to grow blobstore\n");
9856 : 48 : bs_grow_live_done(ctx, 0);
9857 : 48 : return;
9858 : : }
9859 : : /*
9860 : : * Blobstore cannot be shrunk, so check before if:
9861 : : * - new size of the device is smaller than size in super_block
9862 : : * - new total number of clusters is smaller than used_clusters bit_pool
9863 : : * - there is enough space in metadata for used_cluster_mask to be written out
9864 : : */
9865 [ + - ]: 38 : if (dev_size < ctx->super->size ||
9866 [ + - + + ]: 38 : total_clusters < spdk_bit_pool_capacity(ctx->bs->used_clusters) ||
9867 : : used_cluster_mask_len > max_used_cluster_mask) {
9868 [ - + - + ]: 12 : SPDK_DEBUGLOG(blob, "No space to grow blobstore\n");
9869 : 12 : bs_grow_live_done(ctx, -ENOSPC);
9870 : 12 : return;
9871 : : }
9872 : :
9873 [ - + - + ]: 26 : SPDK_DEBUGLOG(blob, "Resizing blobstore\n");
9874 : :
9875 : 26 : ctx->new_used_clusters_mask = calloc(1, total_clusters);
9876 [ - + ]: 26 : if (!ctx->new_used_clusters_mask) {
9877 : 0 : bs_grow_live_done(ctx, -ENOMEM);
9878 : 0 : return;
9879 : : }
9880 : 26 : ctx->new_used_clusters = spdk_bit_pool_create(total_clusters);
9881 [ - + ]: 26 : if (!ctx->new_used_clusters) {
9882 : 0 : bs_grow_live_done(ctx, -ENOMEM);
9883 : 0 : return;
9884 : : }
9885 : :
9886 : 26 : ctx->super->clean = 0;
9887 : 26 : ctx->super->size = dev_size;
9888 : 26 : ctx->super->used_cluster_mask_len = used_cluster_mask_len;
9889 : 26 : bs_write_super(seq, ctx->bs, ctx->super, bs_grow_live_super_write_cpl, ctx);
9890 : : }
9891 : :
9892 : : void
9893 : 98 : spdk_bs_grow_live(struct spdk_blob_store *bs,
9894 : : spdk_bs_op_complete cb_fn, void *cb_arg)
9895 : : {
9896 : 98 : struct spdk_bs_cpl cpl;
9897 : : struct spdk_bs_grow_ctx *ctx;
9898 : :
9899 [ - + ]: 98 : assert(spdk_get_thread() == bs->md_thread);
9900 : :
9901 [ - + - + ]: 98 : SPDK_DEBUGLOG(blob, "Growing blobstore on dev %p\n", bs->dev);
9902 : :
9903 : 98 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
9904 : 98 : cpl.u.bs_basic.cb_fn = cb_fn;
9905 : 98 : cpl.u.bs_basic.cb_arg = cb_arg;
9906 : :
9907 : 98 : ctx = calloc(1, sizeof(struct spdk_bs_grow_ctx));
9908 [ - + ]: 98 : if (!ctx) {
9909 : 0 : cb_fn(cb_arg, -ENOMEM);
9910 : 0 : return;
9911 : : }
9912 : 98 : ctx->bs = bs;
9913 : :
9914 : 98 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
9915 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
9916 [ - + ]: 98 : if (!ctx->super) {
9917 : 0 : free(ctx);
9918 : 0 : cb_fn(cb_arg, -ENOMEM);
9919 : 0 : return;
9920 : : }
9921 : :
9922 : 98 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
9923 [ - + ]: 98 : if (!ctx->seq) {
9924 : 0 : spdk_free(ctx->super);
9925 : 0 : free(ctx);
9926 : 0 : cb_fn(cb_arg, -ENOMEM);
9927 : 0 : return;
9928 : : }
9929 : :
9930 : : /* Read the super block */
9931 : 98 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
9932 : 98 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
9933 : : bs_grow_live_load_super_cpl, ctx);
9934 : : }
9935 : :
9936 : : void
9937 : 12 : spdk_bs_grow(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
9938 : : spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
9939 : : {
9940 : 12 : struct spdk_blob_store *bs;
9941 : 12 : struct spdk_bs_cpl cpl;
9942 : 12 : struct spdk_bs_load_ctx *ctx;
9943 : 12 : struct spdk_bs_opts opts = {};
9944 : : int err;
9945 : :
9946 [ - + - + ]: 12 : SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
9947 : :
9948 [ - + - + ]: 12 : if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
9949 [ # # # # ]: 0 : SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
9950 : 0 : dev->destroy(dev);
9951 : 0 : cb_fn(cb_arg, NULL, -EINVAL);
9952 : 0 : return;
9953 : : }
9954 : :
9955 : 12 : spdk_bs_opts_init(&opts, sizeof(opts));
9956 [ + - ]: 12 : if (o) {
9957 [ - + ]: 12 : if (bs_opts_copy(o, &opts)) {
9958 : 0 : return;
9959 : : }
9960 : : }
9961 : :
9962 [ + - - + ]: 12 : if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
9963 : 0 : dev->destroy(dev);
9964 : 0 : cb_fn(cb_arg, NULL, -EINVAL);
9965 : 0 : return;
9966 : : }
9967 : :
9968 : 12 : err = bs_alloc(dev, &opts, &bs, &ctx);
9969 [ - + ]: 12 : if (err) {
9970 : 0 : dev->destroy(dev);
9971 : 0 : cb_fn(cb_arg, NULL, err);
9972 : 0 : return;
9973 : : }
9974 : :
9975 : 12 : cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
9976 : 12 : cpl.u.bs_handle.cb_fn = cb_fn;
9977 : 12 : cpl.u.bs_handle.cb_arg = cb_arg;
9978 : 12 : cpl.u.bs_handle.bs = bs;
9979 : :
9980 : 12 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
9981 [ - + ]: 12 : if (!ctx->seq) {
9982 : 0 : spdk_free(ctx->super);
9983 : 0 : free(ctx);
9984 : 0 : bs_free(bs);
9985 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
9986 : 0 : return;
9987 : : }
9988 : :
9989 : : /* Read the super block */
9990 : 12 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
9991 : 12 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
9992 : : bs_grow_load_super_cpl, ctx);
9993 : : }
9994 : :
9995 : : int
9996 : 101 : spdk_blob_get_esnap_id(struct spdk_blob *blob, const void **id, size_t *len)
9997 : : {
9998 [ + + ]: 101 : if (!blob_is_esnap_clone(blob)) {
9999 : 38 : return -EINVAL;
10000 : : }
10001 : :
10002 : 63 : return blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, id, len, true);
10003 : : }
10004 : :
10005 : : struct spdk_io_channel *
10006 : 40295 : blob_esnap_get_io_channel(struct spdk_io_channel *ch, struct spdk_blob *blob)
10007 : : {
10008 : 40295 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(ch);
10009 : 40295 : struct spdk_bs_dev *bs_dev = blob->back_bs_dev;
10010 : 40295 : struct blob_esnap_channel find = {};
10011 : : struct blob_esnap_channel *esnap_channel, *existing;
10012 : :
10013 : 40295 : find.blob_id = blob->id;
10014 : 40295 : esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
10015 [ + + ]: 40295 : if (spdk_likely(esnap_channel != NULL)) {
10016 [ - + - + ]: 40142 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": using cached channel on thread %s\n",
10017 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
10018 : 40142 : return esnap_channel->channel;
10019 : : }
10020 : :
10021 [ - + - + ]: 153 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": allocating channel on thread %s\n",
10022 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
10023 : :
10024 : 153 : esnap_channel = calloc(1, sizeof(*esnap_channel));
10025 [ - + ]: 153 : if (esnap_channel == NULL) {
10026 : 0 : SPDK_NOTICELOG("blob 0x%" PRIx64 " channel allocation failed: no memory\n",
10027 : : find.blob_id);
10028 : 0 : return NULL;
10029 : : }
10030 : 153 : esnap_channel->channel = bs_dev->create_channel(bs_dev);
10031 [ - + ]: 153 : if (esnap_channel->channel == NULL) {
10032 : 0 : SPDK_NOTICELOG("blob 0x%" PRIx64 " back channel allocation failed\n", blob->id);
10033 : 0 : free(esnap_channel);
10034 : 0 : return NULL;
10035 : : }
10036 : 153 : esnap_channel->blob_id = find.blob_id;
10037 : 153 : existing = RB_INSERT(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
10038 [ - + ]: 153 : if (spdk_unlikely(existing != NULL)) {
10039 : : /*
10040 : : * This should be unreachable: all modifications to this tree happen on this thread.
10041 : : */
10042 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 "lost race to allocate a channel\n", find.blob_id);
10043 : 0 : assert(false);
10044 : :
10045 : : bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
10046 : : free(esnap_channel);
10047 : :
10048 : : return existing->channel;
10049 : : }
10050 : :
10051 : 153 : return esnap_channel->channel;
10052 : : }
10053 : :
10054 : : static int
10055 : 40223 : blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2)
10056 : : {
10057 [ + - ]: 40223 : return (c1->blob_id < c2->blob_id ? -1 : c1->blob_id > c2->blob_id);
10058 : : }
10059 : :
10060 : : struct blob_esnap_destroy_ctx {
10061 : : spdk_blob_op_with_handle_complete cb_fn;
10062 : : void *cb_arg;
10063 : : struct spdk_blob *blob;
10064 : : struct spdk_bs_dev *back_bs_dev;
10065 : : bool abort_io;
10066 : : };
10067 : :
10068 : : static void
10069 : 451 : blob_esnap_destroy_channels_done(struct spdk_io_channel_iter *i, int status)
10070 : : {
10071 : 451 : struct blob_esnap_destroy_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
10072 : 451 : struct spdk_blob *blob = ctx->blob;
10073 : 451 : struct spdk_blob_store *bs = blob->bs;
10074 : :
10075 [ - + - + ]: 451 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": done destroying channels for this blob\n",
10076 : : blob->id);
10077 : :
10078 [ + + ]: 451 : if (ctx->cb_fn != NULL) {
10079 : 411 : ctx->cb_fn(ctx->cb_arg, blob, status);
10080 : : }
10081 : 451 : free(ctx);
10082 : :
10083 : 451 : bs->esnap_channels_unloading--;
10084 [ + - + + ]: 451 : if (bs->esnap_channels_unloading == 0 && bs->esnap_unload_cb_fn != NULL) {
10085 : 28 : spdk_bs_unload(bs, bs->esnap_unload_cb_fn, bs->esnap_unload_cb_arg);
10086 : : }
10087 : 451 : }
10088 : :
10089 : : static void
10090 : 475 : blob_esnap_destroy_one_channel(struct spdk_io_channel_iter *i)
10091 : : {
10092 : 475 : struct blob_esnap_destroy_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
10093 : 475 : struct spdk_blob *blob = ctx->blob;
10094 : 475 : struct spdk_bs_dev *bs_dev = ctx->back_bs_dev;
10095 : 475 : struct spdk_io_channel *channel = spdk_io_channel_iter_get_channel(i);
10096 : 475 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(channel);
10097 : : struct blob_esnap_channel *esnap_channel;
10098 : 475 : struct blob_esnap_channel find = {};
10099 : :
10100 [ - + ]: 475 : assert(spdk_get_thread() == spdk_io_channel_get_thread(channel));
10101 : :
10102 : 475 : find.blob_id = blob->id;
10103 : 475 : esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
10104 [ + + ]: 475 : if (esnap_channel != NULL) {
10105 [ - + - + ]: 57 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channel on thread %s\n",
10106 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
10107 : 57 : RB_REMOVE(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
10108 : :
10109 [ + + + + ]: 57 : if (ctx->abort_io) {
10110 : : spdk_bs_user_op_t *op, *tmp;
10111 : :
10112 [ - + ]: 26 : TAILQ_FOREACH_SAFE(op, &bs_channel->queued_io, link, tmp) {
10113 [ # # ]: 0 : if (op->back_channel == esnap_channel->channel) {
10114 [ # # ]: 0 : TAILQ_REMOVE(&bs_channel->queued_io, op, link);
10115 : 0 : bs_user_op_abort(op, -EIO);
10116 : : }
10117 : : }
10118 : : }
10119 : :
10120 : 57 : bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
10121 : 57 : free(esnap_channel);
10122 : : }
10123 : :
10124 : 475 : spdk_for_each_channel_continue(i, 0);
10125 : 475 : }
10126 : :
10127 : : /*
10128 : : * Destroy the channels for a specific blob on each thread with a blobstore channel. This should be
10129 : : * used when closing an esnap clone blob and after decoupling from the parent.
10130 : : */
10131 : : static void
10132 : 1586 : blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
10133 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
10134 : : {
10135 : : struct blob_esnap_destroy_ctx *ctx;
10136 : :
10137 [ + + + + ]: 1586 : if (!blob_is_esnap_clone(blob) || blob->back_bs_dev == NULL) {
10138 [ + - ]: 1135 : if (cb_fn != NULL) {
10139 : 1135 : cb_fn(cb_arg, blob, 0);
10140 : : }
10141 : 1135 : return;
10142 : : }
10143 : :
10144 : 451 : ctx = calloc(1, sizeof(*ctx));
10145 [ - + ]: 451 : if (ctx == NULL) {
10146 [ # # ]: 0 : if (cb_fn != NULL) {
10147 : 0 : cb_fn(cb_arg, blob, -ENOMEM);
10148 : : }
10149 : 0 : return;
10150 : : }
10151 : 451 : ctx->cb_fn = cb_fn;
10152 : 451 : ctx->cb_arg = cb_arg;
10153 : 451 : ctx->blob = blob;
10154 : 451 : ctx->back_bs_dev = blob->back_bs_dev;
10155 : 451 : ctx->abort_io = abort_io;
10156 : :
10157 [ - + - + ]: 451 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channels for this blob\n",
10158 : : blob->id);
10159 : :
10160 : 451 : blob->bs->esnap_channels_unloading++;
10161 : 451 : spdk_for_each_channel(blob->bs, blob_esnap_destroy_one_channel, ctx,
10162 : : blob_esnap_destroy_channels_done);
10163 : : }
10164 : :
10165 : : /*
10166 : : * Destroy all bs_dev channels on a specific blobstore channel. This should be used when a
10167 : : * bs_channel is destroyed.
10168 : : */
10169 : : static void
10170 : 9841 : blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch)
10171 : : {
10172 : : struct blob_esnap_channel *esnap_channel, *esnap_channel_tmp;
10173 : :
10174 [ - + ]: 9841 : assert(spdk_get_thread() == spdk_io_channel_get_thread(spdk_io_channel_from_ctx(ch)));
10175 : :
10176 [ - + - + ]: 9841 : SPDK_DEBUGLOG(blob_esnap, "destroying channels on thread %s\n",
10177 : : spdk_thread_get_name(spdk_get_thread()));
10178 [ + + + - ]: 9937 : RB_FOREACH_SAFE(esnap_channel, blob_esnap_channel_tree, &ch->esnap_channels,
10179 : : esnap_channel_tmp) {
10180 [ - + - + ]: 96 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64
10181 : : ": destroying one channel in thread %s\n",
10182 : : esnap_channel->blob_id, spdk_thread_get_name(spdk_get_thread()));
10183 : 96 : RB_REMOVE(blob_esnap_channel_tree, &ch->esnap_channels, esnap_channel);
10184 : 96 : spdk_put_io_channel(esnap_channel->channel);
10185 : 96 : free(esnap_channel);
10186 : : }
10187 [ - + - + ]: 9841 : SPDK_DEBUGLOG(blob_esnap, "done destroying channels on thread %s\n",
10188 : : spdk_thread_get_name(spdk_get_thread()));
10189 : 9841 : }
10190 : :
10191 : : static void
10192 : 93 : blob_set_back_bs_dev_done(void *_ctx, int bserrno)
10193 : : {
10194 : 93 : struct set_bs_dev_ctx *ctx = _ctx;
10195 : :
10196 [ - + ]: 93 : if (bserrno != 0) {
10197 : : /* Even though the unfreeze failed, the update may have succeed. */
10198 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": unfreeze failed with error %d\n", ctx->blob->id,
10199 : : bserrno);
10200 : : }
10201 : 93 : ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
10202 : 93 : free(ctx);
10203 : 93 : }
10204 : :
10205 : : static void
10206 : 93 : blob_frozen_set_back_bs_dev(void *_ctx, struct spdk_blob *blob, int bserrno)
10207 : : {
10208 : 93 : struct set_bs_dev_ctx *ctx = _ctx;
10209 : : int rc;
10210 : :
10211 [ - + ]: 93 : if (bserrno != 0) {
10212 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to release old back_bs_dev with error %d\n",
10213 : : blob->id, bserrno);
10214 : 0 : ctx->bserrno = bserrno;
10215 : 0 : blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
10216 : 0 : return;
10217 : : }
10218 : :
10219 [ + - ]: 93 : if (blob->back_bs_dev != NULL) {
10220 : 93 : blob->back_bs_dev->destroy(blob->back_bs_dev);
10221 : 93 : blob->back_bs_dev = NULL;
10222 : : }
10223 : :
10224 [ + + ]: 93 : if (ctx->parent_refs_cb_fn) {
10225 : 66 : rc = ctx->parent_refs_cb_fn(blob, ctx->parent_refs_cb_arg);
10226 [ - + ]: 66 : if (rc != 0) {
10227 : 0 : ctx->bserrno = rc;
10228 : 0 : blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
10229 : 0 : return;
10230 : : }
10231 : : }
10232 : :
10233 : 93 : SPDK_NOTICELOG("blob 0x%" PRIx64 ": hotplugged back_bs_dev\n", blob->id);
10234 : 93 : blob->back_bs_dev = ctx->back_bs_dev;
10235 : 93 : ctx->bserrno = 0;
10236 : :
10237 : 93 : blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
10238 : : }
10239 : :
10240 : : static void
10241 : 93 : blob_set_back_bs_dev_frozen(void *_ctx, int bserrno)
10242 : : {
10243 : 93 : struct set_bs_dev_ctx *ctx = _ctx;
10244 : 93 : struct spdk_blob *blob = ctx->blob;
10245 : :
10246 [ - + ]: 93 : if (bserrno != 0) {
10247 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to freeze with error %d\n", blob->id,
10248 : : bserrno);
10249 : 0 : ctx->cb_fn(ctx->cb_arg, bserrno);
10250 : 0 : free(ctx);
10251 : 0 : return;
10252 : : }
10253 : :
10254 : : /*
10255 : : * This does not prevent future reads from the esnap device because any future IO will
10256 : : * lazily create a new esnap IO channel.
10257 : : */
10258 : 93 : blob_esnap_destroy_bs_dev_channels(blob, true, blob_frozen_set_back_bs_dev, ctx);
10259 : : }
10260 : :
10261 : : void
10262 : 27 : spdk_blob_set_esnap_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
10263 : : spdk_blob_op_complete cb_fn, void *cb_arg)
10264 : : {
10265 [ - + ]: 27 : if (!blob_is_esnap_clone(blob)) {
10266 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
10267 : 0 : cb_fn(cb_arg, -EINVAL);
10268 : 0 : return;
10269 : : }
10270 : :
10271 : 27 : blob_set_back_bs_dev(blob, back_bs_dev, NULL, NULL, cb_fn, cb_arg);
10272 : : }
10273 : :
10274 : : struct spdk_bs_dev *
10275 : 164 : spdk_blob_get_esnap_bs_dev(const struct spdk_blob *blob)
10276 : : {
10277 [ - + ]: 164 : if (!blob_is_esnap_clone(blob)) {
10278 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
10279 : 0 : return NULL;
10280 : : }
10281 : :
10282 : 164 : return blob->back_bs_dev;
10283 : : }
10284 : :
10285 : : bool
10286 : 851 : spdk_blob_is_degraded(const struct spdk_blob *blob)
10287 : : {
10288 [ + + + + ]: 851 : if (blob->bs->dev->is_degraded != NULL && blob->bs->dev->is_degraded(blob->bs->dev)) {
10289 : 12 : return true;
10290 : : }
10291 [ + + + + ]: 839 : if (blob->back_bs_dev == NULL || blob->back_bs_dev->is_degraded == NULL) {
10292 : 677 : return false;
10293 : : }
10294 : :
10295 : 162 : return blob->back_bs_dev->is_degraded(blob->back_bs_dev);
10296 : : }
10297 : :
10298 : 2156 : SPDK_LOG_REGISTER_COMPONENT(blob)
10299 : 2156 : SPDK_LOG_REGISTER_COMPONENT(blob_esnap)
|