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 : : #include "spdk/trace.h"
20 : :
21 : : #include "spdk_internal/assert.h"
22 : : #include "spdk_internal/trace_defs.h"
23 : : #include "spdk/log.h"
24 : :
25 : : #include "blobstore.h"
26 : :
27 : : #define BLOB_CRC32C_INITIAL 0xffffffffUL
28 : :
29 : : static int bs_register_md_thread(struct spdk_blob_store *bs);
30 : : static int bs_unregister_md_thread(struct spdk_blob_store *bs);
31 : : static void blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
32 : : static void blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
33 : : uint64_t cluster, uint32_t extent, struct spdk_blob_md_page *page,
34 : : spdk_blob_op_complete cb_fn, void *cb_arg);
35 : : static void blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
36 : : uint32_t extent_page, struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
37 : :
38 : : static int blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
39 : : uint16_t value_len, bool internal);
40 : : static int blob_get_xattr_value(struct spdk_blob *blob, const char *name,
41 : : const void **value, size_t *value_len, bool internal);
42 : : static int blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal);
43 : :
44 : : static void blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
45 : : struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
46 : : static void blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg);
47 : :
48 : : static void bs_shallow_copy_cluster_find_next(void *cb_arg);
49 : :
50 : : /*
51 : : * External snapshots require a channel per thread per esnap bdev. The tree
52 : : * is populated lazily as blob IOs are handled by the back_bs_dev. When this
53 : : * channel is destroyed, all the channels in the tree are destroyed.
54 : : */
55 : :
56 : : struct blob_esnap_channel {
57 : : RB_ENTRY(blob_esnap_channel) node;
58 : : spdk_blob_id blob_id;
59 : : struct spdk_io_channel *channel;
60 : : };
61 : :
62 : : static int blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2);
63 : : static void blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
64 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg);
65 : : static void blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch);
66 : : static void blob_set_back_bs_dev_frozen(void *_ctx, int bserrno);
67 [ + + + + : 22203 : RB_GENERATE_STATIC(blob_esnap_channel_tree, blob_esnap_channel, node, blob_esnap_channel_compare)
+ + + - +
+ + - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + #
# # # # #
# # # # #
# # # ]
68 : :
69 : : static inline bool
70 : 100357 : blob_is_esnap_clone(const struct spdk_blob *blob)
71 : : {
72 [ - + ]: 100357 : assert(blob != NULL);
73 : 100357 : return !!(blob->invalid_flags & SPDK_BLOB_EXTERNAL_SNAPSHOT);
74 : : }
75 : :
76 : : static int
77 : 4618 : blob_id_cmp(struct spdk_blob *blob1, struct spdk_blob *blob2)
78 : : {
79 [ + - + + ]: 4618 : assert(blob1 != NULL && blob2 != NULL);
80 [ + + ]: 4618 : return (blob1->id < blob2->id ? -1 : blob1->id > blob2->id);
81 : : }
82 : :
83 [ + + + + : 32148 : RB_GENERATE_STATIC(spdk_blob_tree, spdk_blob, link, blob_id_cmp);
+ + + + +
+ + + + +
+ + + + -
+ + + + +
+ + + + +
+ + + - +
+ + + + +
+ + + + +
- + + + -
- - - - -
+ + + - -
- - + ]
84 : :
85 : : static void
86 : 74397 : blob_verify_md_op(struct spdk_blob *blob)
87 : : {
88 [ - + ]: 74397 : assert(blob != NULL);
89 [ - + ]: 74397 : assert(spdk_get_thread() == blob->bs->md_thread);
90 [ - + ]: 74397 : assert(blob->state != SPDK_BLOB_STATE_LOADING);
91 : 74397 : }
92 : :
93 : : static struct spdk_blob_list *
94 : 7698 : bs_get_snapshot_entry(struct spdk_blob_store *bs, spdk_blob_id blobid)
95 : : {
96 : 7698 : struct spdk_blob_list *snapshot_entry = NULL;
97 : :
98 [ + + ]: 9686 : TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
99 [ + + ]: 3544 : if (snapshot_entry->id == blobid) {
100 : 1556 : break;
101 : : }
102 : : }
103 : :
104 : 7698 : return snapshot_entry;
105 : : }
106 : :
107 : : static void
108 : 5833 : bs_claim_md_page(struct spdk_blob_store *bs, uint32_t page)
109 : : {
110 [ - + ]: 5833 : assert(spdk_spin_held(&bs->used_lock));
111 [ - + ]: 5833 : assert(page < spdk_bit_array_capacity(bs->used_md_pages));
112 [ - + ]: 5833 : assert(spdk_bit_array_get(bs->used_md_pages, page) == false);
113 : :
114 : 5833 : spdk_bit_array_set(bs->used_md_pages, page);
115 : 5833 : }
116 : :
117 : : static void
118 : 4404 : bs_release_md_page(struct spdk_blob_store *bs, uint32_t page)
119 : : {
120 [ - + ]: 4404 : assert(spdk_spin_held(&bs->used_lock));
121 [ - + ]: 4404 : assert(page < spdk_bit_array_capacity(bs->used_md_pages));
122 [ - + ]: 4404 : assert(spdk_bit_array_get(bs->used_md_pages, page) == true);
123 : :
124 : 4404 : spdk_bit_array_clear(bs->used_md_pages, page);
125 : 4404 : }
126 : :
127 : : static uint32_t
128 : 16515 : bs_claim_cluster(struct spdk_blob_store *bs)
129 : : {
130 : : uint32_t cluster_num;
131 : :
132 [ - + ]: 16515 : assert(spdk_spin_held(&bs->used_lock));
133 : :
134 : 16515 : cluster_num = spdk_bit_pool_allocate_bit(bs->used_clusters);
135 [ - + ]: 16515 : if (cluster_num == UINT32_MAX) {
136 : 0 : return UINT32_MAX;
137 : : }
138 : :
139 [ - + ]: 16515 : SPDK_DEBUGLOG(blob, "Claiming cluster %u\n", cluster_num);
140 : 16515 : bs->num_free_clusters--;
141 : :
142 : 16515 : return cluster_num;
143 : : }
144 : :
145 : : static void
146 : 4862 : bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
147 : : {
148 [ - + ]: 4862 : assert(spdk_spin_held(&bs->used_lock));
149 [ - + ]: 4862 : assert(cluster_num < spdk_bit_pool_capacity(bs->used_clusters));
150 [ - + ]: 4862 : assert(spdk_bit_pool_is_allocated(bs->used_clusters, cluster_num) == true);
151 [ - + ]: 4862 : assert(bs->num_free_clusters < bs->total_clusters);
152 : :
153 [ - + ]: 4862 : SPDK_DEBUGLOG(blob, "Releasing cluster %u\n", cluster_num);
154 : :
155 : 4862 : spdk_bit_pool_free_bit(bs->used_clusters, cluster_num);
156 : 4862 : bs->num_free_clusters++;
157 : 4862 : }
158 : :
159 : : static int
160 : 16515 : blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster)
161 : : {
162 : 16515 : uint64_t *cluster_lba = &blob->active.clusters[cluster_num];
163 : :
164 : 16515 : blob_verify_md_op(blob);
165 : :
166 [ + + ]: 16515 : if (*cluster_lba != 0) {
167 : 8 : return -EEXIST;
168 : : }
169 : :
170 : 16507 : *cluster_lba = bs_cluster_to_lba(blob->bs, cluster);
171 : 16507 : blob->active.num_allocated_clusters++;
172 : :
173 : 16507 : return 0;
174 : : }
175 : :
176 : : static int
177 : 16515 : bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num,
178 : : uint64_t *cluster, uint32_t *lowest_free_md_page, bool update_map)
179 : : {
180 : 16515 : uint32_t *extent_page = 0;
181 : :
182 [ - + ]: 16515 : assert(spdk_spin_held(&blob->bs->used_lock));
183 : :
184 : 16515 : *cluster = bs_claim_cluster(blob->bs);
185 [ - + ]: 16515 : if (*cluster == UINT32_MAX) {
186 : : /* No more free clusters. Cannot satisfy the request */
187 : 0 : return -ENOSPC;
188 : : }
189 : :
190 [ + + ]: 16515 : if (blob->use_extent_table) {
191 : 8411 : extent_page = bs_cluster_to_extent_page(blob, cluster_num);
192 [ + + ]: 8411 : if (*extent_page == 0) {
193 : : /* Extent page shall never occupy md_page so start the search from 1 */
194 [ + + ]: 1465 : if (*lowest_free_md_page == 0) {
195 : 1461 : *lowest_free_md_page = 1;
196 : : }
197 : : /* No extent_page is allocated for the cluster */
198 : 1465 : *lowest_free_md_page = spdk_bit_array_find_first_clear(blob->bs->used_md_pages,
199 : : *lowest_free_md_page);
200 [ - + ]: 1465 : if (*lowest_free_md_page == UINT32_MAX) {
201 : : /* No more free md pages. Cannot satisfy the request */
202 : 0 : bs_release_cluster(blob->bs, *cluster);
203 : 0 : return -ENOSPC;
204 : : }
205 : 1465 : bs_claim_md_page(blob->bs, *lowest_free_md_page);
206 : : }
207 : : }
208 : :
209 [ - + ]: 16515 : SPDK_DEBUGLOG(blob, "Claiming cluster %" PRIu64 " for blob 0x%" PRIx64 "\n", *cluster,
210 : : blob->id);
211 : :
212 [ + + ]: 16515 : if (update_map) {
213 : 14877 : blob_insert_cluster(blob, cluster_num, *cluster);
214 [ + + + + ]: 14877 : if (blob->use_extent_table && *extent_page == 0) {
215 : 1295 : *extent_page = *lowest_free_md_page;
216 : : }
217 : : }
218 : :
219 : 16515 : return 0;
220 : : }
221 : :
222 : : static void
223 : 11203 : blob_xattrs_init(struct spdk_blob_xattr_opts *xattrs)
224 : : {
225 : 11203 : xattrs->count = 0;
226 : 11203 : xattrs->names = NULL;
227 : 11203 : xattrs->ctx = NULL;
228 : 11203 : xattrs->get_value = NULL;
229 : 11203 : }
230 : :
231 : : void
232 : 7399 : spdk_blob_opts_init(struct spdk_blob_opts *opts, size_t opts_size)
233 : : {
234 [ - + ]: 7399 : if (!opts) {
235 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
236 : 0 : return;
237 : : }
238 : :
239 [ - + ]: 7399 : if (!opts_size) {
240 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
241 : 0 : return;
242 : : }
243 : :
244 : 7399 : memset(opts, 0, opts_size);
245 : 7399 : opts->opts_size = opts_size;
246 : :
247 : : #define FIELD_OK(field) \
248 : : offsetof(struct spdk_blob_opts, field) + sizeof(opts->field) <= opts_size
249 : :
250 : : #define SET_FIELD(field, value) \
251 : : if (FIELD_OK(field)) { \
252 : : opts->field = value; \
253 : : } \
254 : :
255 [ + - ]: 7399 : SET_FIELD(num_clusters, 0);
256 [ + - ]: 7399 : SET_FIELD(thin_provision, false);
257 [ + - ]: 7399 : SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
258 : :
259 [ + - ]: 7399 : if (FIELD_OK(xattrs)) {
260 : 7399 : blob_xattrs_init(&opts->xattrs);
261 : : }
262 : :
263 [ + - ]: 7399 : SET_FIELD(use_extent_table, true);
264 : :
265 : : #undef FIELD_OK
266 : : #undef SET_FIELD
267 : : }
268 : :
269 : : void
270 : 7061 : spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts, size_t opts_size)
271 : : {
272 [ - + ]: 7061 : if (!opts) {
273 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
274 : 0 : return;
275 : : }
276 : :
277 [ - + ]: 7061 : if (!opts_size) {
278 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
279 : 0 : return;
280 : : }
281 : :
282 : 7061 : memset(opts, 0, opts_size);
283 : 7061 : opts->opts_size = opts_size;
284 : :
285 : : #define FIELD_OK(field) \
286 : : offsetof(struct spdk_blob_open_opts, field) + sizeof(opts->field) <= opts_size
287 : :
288 : : #define SET_FIELD(field, value) \
289 : : if (FIELD_OK(field)) { \
290 : : opts->field = value; \
291 : : } \
292 : :
293 [ + - ]: 7061 : SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
294 : :
295 : : #undef FIELD_OK
296 : : #undef SET_FILED
297 : : }
298 : :
299 : : static struct spdk_blob *
300 : 10832 : blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id)
301 : : {
302 : : struct spdk_blob *blob;
303 : :
304 : 10832 : blob = calloc(1, sizeof(*blob));
305 [ - + ]: 10832 : if (!blob) {
306 : 0 : return NULL;
307 : : }
308 : :
309 : 10832 : blob->id = id;
310 : 10832 : blob->bs = bs;
311 : :
312 : 10832 : blob->parent_id = SPDK_BLOBID_INVALID;
313 : :
314 : 10832 : blob->state = SPDK_BLOB_STATE_DIRTY;
315 : 10832 : blob->extent_rle_found = false;
316 : 10832 : blob->extent_table_found = false;
317 : 10832 : blob->active.num_pages = 1;
318 : 10832 : blob->active.pages = calloc(1, sizeof(*blob->active.pages));
319 [ - + ]: 10832 : if (!blob->active.pages) {
320 : 0 : free(blob);
321 : 0 : return NULL;
322 : : }
323 : :
324 : 10832 : blob->active.pages[0] = bs_blobid_to_page(id);
325 : :
326 : 10832 : TAILQ_INIT(&blob->xattrs);
327 : 10832 : TAILQ_INIT(&blob->xattrs_internal);
328 : 10832 : TAILQ_INIT(&blob->pending_persists);
329 : 10832 : TAILQ_INIT(&blob->persists_to_complete);
330 : :
331 : 10832 : return blob;
332 : : }
333 : :
334 : : static void
335 : 21664 : xattrs_free(struct spdk_xattr_tailq *xattrs)
336 : : {
337 : : struct spdk_xattr *xattr, *xattr_tmp;
338 : :
339 [ + + ]: 25420 : TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) {
340 [ + + ]: 3756 : TAILQ_REMOVE(xattrs, xattr, link);
341 : 3756 : free(xattr->name);
342 : 3756 : free(xattr->value);
343 : 3756 : free(xattr);
344 : : }
345 : 21664 : }
346 : :
347 : : static void
348 : 10832 : blob_free(struct spdk_blob *blob)
349 : : {
350 [ - + ]: 10832 : assert(blob != NULL);
351 [ - + ]: 10832 : assert(TAILQ_EMPTY(&blob->pending_persists));
352 [ - + ]: 10832 : assert(TAILQ_EMPTY(&blob->persists_to_complete));
353 : :
354 : 10832 : free(blob->active.extent_pages);
355 : 10832 : free(blob->clean.extent_pages);
356 : 10832 : free(blob->active.clusters);
357 : 10832 : free(blob->clean.clusters);
358 : 10832 : free(blob->active.pages);
359 : 10832 : free(blob->clean.pages);
360 : :
361 : 10832 : xattrs_free(&blob->xattrs);
362 : 10832 : xattrs_free(&blob->xattrs_internal);
363 : :
364 [ + + ]: 10832 : if (blob->back_bs_dev) {
365 : 2200 : blob->back_bs_dev->destroy(blob->back_bs_dev);
366 : : }
367 : :
368 : 10832 : free(blob);
369 : 10832 : }
370 : :
371 : : static void
372 : 642 : blob_back_bs_destroy_esnap_done(void *ctx, struct spdk_blob *blob, int bserrno)
373 : : {
374 : 642 : struct spdk_bs_dev *bs_dev = ctx;
375 : :
376 [ - + ]: 642 : if (bserrno != 0) {
377 : : /*
378 : : * This is probably due to a memory allocation failure when creating the
379 : : * blob_esnap_destroy_ctx before iterating threads.
380 : : */
381 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": Unable to destroy bs dev channels: error %d\n",
382 : : blob->id, bserrno);
383 : 0 : assert(false);
384 : : }
385 : :
386 [ - + ]: 642 : if (bs_dev == NULL) {
387 : : /*
388 : : * This check exists to make scanbuild happy.
389 : : *
390 : : * blob->back_bs_dev for an esnap is NULL during the first iteration of blobs while
391 : : * the blobstore is being loaded. It could also be NULL if there was an error
392 : : * opening the esnap device. In each of these cases, no channels could have been
393 : : * created because back_bs_dev->create_channel() would have led to a NULL pointer
394 : : * deref.
395 : : */
396 : 0 : assert(false);
397 : : return;
398 : : }
399 : :
400 [ - + ]: 642 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": calling destroy on back_bs_dev\n", blob->id);
401 : 642 : bs_dev->destroy(bs_dev);
402 : : }
403 : :
404 : : static void
405 : 642 : blob_back_bs_destroy(struct spdk_blob *blob)
406 : : {
407 [ - + ]: 642 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": preparing to destroy back_bs_dev\n",
408 : : blob->id);
409 : :
410 : 642 : blob_esnap_destroy_bs_dev_channels(blob, false, blob_back_bs_destroy_esnap_done,
411 : 642 : blob->back_bs_dev);
412 : 642 : blob->back_bs_dev = NULL;
413 : 642 : }
414 : :
415 : : struct blob_parent {
416 : : union {
417 : : struct {
418 : : spdk_blob_id id;
419 : : struct spdk_blob *blob;
420 : : } snapshot;
421 : :
422 : : struct {
423 : : void *id;
424 : : uint32_t id_len;
425 : : struct spdk_bs_dev *back_bs_dev;
426 : : } esnap;
427 : : } u;
428 : : };
429 : :
430 : : typedef int (*set_parent_refs_cb)(struct spdk_blob *blob, struct blob_parent *parent);
431 : :
432 : : struct set_bs_dev_ctx {
433 : : struct spdk_blob *blob;
434 : : struct spdk_bs_dev *back_bs_dev;
435 : :
436 : : /*
437 : : * This callback is used during a set parent operation to change the references
438 : : * to the parent of the blob.
439 : : */
440 : : set_parent_refs_cb parent_refs_cb_fn;
441 : : struct blob_parent *parent_refs_cb_arg;
442 : :
443 : : spdk_blob_op_complete cb_fn;
444 : : void *cb_arg;
445 : : int bserrno;
446 : : };
447 : :
448 : : static void
449 : 56 : blob_set_back_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
450 : : set_parent_refs_cb parent_refs_cb_fn, struct blob_parent *parent_refs_cb_arg,
451 : : spdk_blob_op_complete cb_fn, void *cb_arg)
452 : : {
453 : : struct set_bs_dev_ctx *ctx;
454 : :
455 : 56 : ctx = calloc(1, sizeof(*ctx));
456 [ - + ]: 56 : if (ctx == NULL) {
457 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": out of memory while setting back_bs_dev\n",
458 : : blob->id);
459 : 0 : cb_fn(cb_arg, -ENOMEM);
460 : 0 : return;
461 : : }
462 : :
463 : 56 : ctx->parent_refs_cb_fn = parent_refs_cb_fn;
464 : 56 : ctx->parent_refs_cb_arg = parent_refs_cb_arg;
465 : 56 : ctx->cb_fn = cb_fn;
466 : 56 : ctx->cb_arg = cb_arg;
467 : 56 : ctx->back_bs_dev = back_bs_dev;
468 : 56 : ctx->blob = blob;
469 : :
470 : 56 : blob_freeze_io(blob, blob_set_back_bs_dev_frozen, ctx);
471 : : }
472 : :
473 : : struct freeze_io_ctx {
474 : : struct spdk_bs_cpl cpl;
475 : : struct spdk_blob *blob;
476 : : };
477 : :
478 : : static void
479 : 1054 : blob_io_sync(struct spdk_io_channel_iter *i)
480 : : {
481 : 1054 : spdk_for_each_channel_continue(i, 0);
482 : 1054 : }
483 : :
484 : : static void
485 : 1030 : blob_execute_queued_io(struct spdk_io_channel_iter *i)
486 : : {
487 : 1030 : struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
488 : 1030 : struct spdk_bs_channel *ch = spdk_io_channel_get_ctx(_ch);
489 : 1030 : struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
490 : : struct spdk_bs_request_set *set;
491 : : struct spdk_bs_user_op_args *args;
492 : : spdk_bs_user_op_t *op, *tmp;
493 : :
494 [ + + ]: 1038 : TAILQ_FOREACH_SAFE(op, &ch->queued_io, link, tmp) {
495 : 8 : set = (struct spdk_bs_request_set *)op;
496 : 8 : args = &set->u.user_op;
497 : :
498 [ + - ]: 8 : if (args->blob == ctx->blob) {
499 [ - + ]: 8 : TAILQ_REMOVE(&ch->queued_io, op, link);
500 : 8 : bs_user_op_execute(op);
501 : : }
502 : : }
503 : :
504 : 1030 : spdk_for_each_channel_continue(i, 0);
505 : 1030 : }
506 : :
507 : : static void
508 : 2020 : blob_io_cpl(struct spdk_io_channel_iter *i, int status)
509 : : {
510 : 2020 : struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
511 : :
512 : 2020 : ctx->cpl.u.blob_basic.cb_fn(ctx->cpl.u.blob_basic.cb_arg, 0);
513 : :
514 : 2020 : free(ctx);
515 : 2020 : }
516 : :
517 : : static void
518 : 1022 : blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
519 : : {
520 : : struct freeze_io_ctx *ctx;
521 : :
522 : 1022 : blob_verify_md_op(blob);
523 : :
524 : 1022 : ctx = calloc(1, sizeof(*ctx));
525 [ - + ]: 1022 : if (!ctx) {
526 : 0 : cb_fn(cb_arg, -ENOMEM);
527 : 0 : return;
528 : : }
529 : :
530 : 1022 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
531 : 1022 : ctx->cpl.u.blob_basic.cb_fn = cb_fn;
532 : 1022 : ctx->cpl.u.blob_basic.cb_arg = cb_arg;
533 : 1022 : ctx->blob = blob;
534 : :
535 : : /* Freeze I/O on blob */
536 : 1022 : blob->frozen_refcnt++;
537 : :
538 : 1022 : spdk_for_each_channel(blob->bs, blob_io_sync, ctx, blob_io_cpl);
539 : : }
540 : :
541 : : static void
542 : 998 : blob_unfreeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
543 : : {
544 : : struct freeze_io_ctx *ctx;
545 : :
546 : 998 : blob_verify_md_op(blob);
547 : :
548 : 998 : ctx = calloc(1, sizeof(*ctx));
549 [ - + ]: 998 : if (!ctx) {
550 : 0 : cb_fn(cb_arg, -ENOMEM);
551 : 0 : return;
552 : : }
553 : :
554 : 998 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
555 : 998 : ctx->cpl.u.blob_basic.cb_fn = cb_fn;
556 : 998 : ctx->cpl.u.blob_basic.cb_arg = cb_arg;
557 : 998 : ctx->blob = blob;
558 : :
559 [ - + ]: 998 : assert(blob->frozen_refcnt > 0);
560 : :
561 : 998 : blob->frozen_refcnt--;
562 : :
563 : 998 : spdk_for_each_channel(blob->bs, blob_execute_queued_io, ctx, blob_io_cpl);
564 : : }
565 : :
566 : : static int
567 : 17031 : blob_mark_clean(struct spdk_blob *blob)
568 : : {
569 : 17031 : uint32_t *extent_pages = NULL;
570 : 17031 : uint64_t *clusters = NULL;
571 : 17031 : uint32_t *pages = NULL;
572 : :
573 [ - + ]: 17031 : assert(blob != NULL);
574 : :
575 [ + + ]: 17031 : if (blob->active.num_extent_pages) {
576 [ - + ]: 5780 : assert(blob->active.extent_pages);
577 : 5780 : extent_pages = calloc(blob->active.num_extent_pages, sizeof(*blob->active.extent_pages));
578 [ - + ]: 5780 : if (!extent_pages) {
579 : 0 : return -ENOMEM;
580 : : }
581 : 5780 : memcpy(extent_pages, blob->active.extent_pages,
582 : 5780 : blob->active.num_extent_pages * sizeof(*extent_pages));
583 : : }
584 : :
585 [ + + ]: 17031 : if (blob->active.num_clusters) {
586 [ - + ]: 11926 : assert(blob->active.clusters);
587 : 11926 : clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters));
588 [ - + ]: 11926 : if (!clusters) {
589 : 0 : free(extent_pages);
590 : 0 : return -ENOMEM;
591 : : }
592 : 11926 : memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
593 : : }
594 : :
595 [ + + ]: 17031 : if (blob->active.num_pages) {
596 [ - + ]: 14053 : assert(blob->active.pages);
597 : 14053 : pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages));
598 [ - + ]: 14053 : if (!pages) {
599 : 0 : free(extent_pages);
600 : 0 : free(clusters);
601 : 0 : return -ENOMEM;
602 : : }
603 : 14053 : memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
604 : : }
605 : :
606 : 17031 : free(blob->clean.extent_pages);
607 : 17031 : free(blob->clean.clusters);
608 : 17031 : free(blob->clean.pages);
609 : :
610 : 17031 : blob->clean.num_extent_pages = blob->active.num_extent_pages;
611 : 17031 : blob->clean.extent_pages = blob->active.extent_pages;
612 : 17031 : blob->clean.num_clusters = blob->active.num_clusters;
613 : 17031 : blob->clean.clusters = blob->active.clusters;
614 : 17031 : blob->clean.num_allocated_clusters = blob->active.num_allocated_clusters;
615 : 17031 : blob->clean.num_pages = blob->active.num_pages;
616 : 17031 : blob->clean.pages = blob->active.pages;
617 : :
618 : 17031 : blob->active.extent_pages = extent_pages;
619 : 17031 : blob->active.clusters = clusters;
620 : 17031 : blob->active.pages = pages;
621 : :
622 : : /* If the metadata was dirtied again while the metadata was being written to disk,
623 : : * we do not want to revert the DIRTY state back to CLEAN here.
624 : : */
625 [ + + ]: 17031 : if (blob->state == SPDK_BLOB_STATE_LOADING) {
626 : 6900 : blob->state = SPDK_BLOB_STATE_CLEAN;
627 : : }
628 : :
629 : 17031 : return 0;
630 : : }
631 : :
632 : : static int
633 : 2748 : blob_deserialize_xattr(struct spdk_blob *blob,
634 : : struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal)
635 : : {
636 : : struct spdk_xattr *xattr;
637 : :
638 : 2748 : if (desc_xattr->length != sizeof(desc_xattr->name_length) +
639 : : sizeof(desc_xattr->value_length) +
640 [ - + ]: 2748 : desc_xattr->name_length + desc_xattr->value_length) {
641 : 0 : return -EINVAL;
642 : : }
643 : :
644 : 2748 : xattr = calloc(1, sizeof(*xattr));
645 [ - + ]: 2748 : if (xattr == NULL) {
646 : 0 : return -ENOMEM;
647 : : }
648 : :
649 : 2748 : xattr->name = malloc(desc_xattr->name_length + 1);
650 [ - + ]: 2748 : if (xattr->name == NULL) {
651 : 0 : free(xattr);
652 : 0 : return -ENOMEM;
653 : : }
654 : :
655 : 2748 : xattr->value = malloc(desc_xattr->value_length);
656 [ - + ]: 2748 : if (xattr->value == NULL) {
657 : 0 : free(xattr->name);
658 : 0 : free(xattr);
659 : 0 : return -ENOMEM;
660 : : }
661 : :
662 : 2748 : memcpy(xattr->name, desc_xattr->name, desc_xattr->name_length);
663 : 2748 : xattr->name[desc_xattr->name_length] = '\0';
664 : 2748 : xattr->value_len = desc_xattr->value_length;
665 : 2748 : memcpy(xattr->value,
666 : 2748 : (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
667 : 2748 : desc_xattr->value_length);
668 : :
669 [ + + + + : 2748 : TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link);
+ + ]
670 : :
671 : 2748 : return 0;
672 : : }
673 : :
674 : :
675 : : static int
676 : 9289 : blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob)
677 : : {
678 : : struct spdk_blob_md_descriptor *desc;
679 : 9289 : size_t cur_desc = 0;
680 : : void *tmp;
681 : :
682 : 9289 : desc = (struct spdk_blob_md_descriptor *)page->descriptors;
683 [ + - ]: 27438 : while (cur_desc < sizeof(page->descriptors)) {
684 [ + + ]: 27438 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
685 [ + - ]: 9193 : if (desc->length == 0) {
686 : : /* If padding and length are 0, this terminates the page */
687 : 9193 : break;
688 : : }
689 [ + + ]: 18245 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
690 : : struct spdk_blob_md_descriptor_flags *desc_flags;
691 : :
692 : 6964 : desc_flags = (struct spdk_blob_md_descriptor_flags *)desc;
693 : :
694 [ - + ]: 6964 : if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) {
695 : 0 : return -EINVAL;
696 : : }
697 : :
698 [ + + ]: 6964 : if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) !=
699 : : SPDK_BLOB_INVALID_FLAGS_MASK) {
700 : 16 : return -EINVAL;
701 : : }
702 : :
703 [ + + ]: 6948 : if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) !=
704 : : SPDK_BLOB_DATA_RO_FLAGS_MASK) {
705 : 24 : blob->data_ro = true;
706 : 24 : blob->md_ro = true;
707 : : }
708 : :
709 [ + + ]: 6948 : if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) !=
710 : : SPDK_BLOB_MD_RO_FLAGS_MASK) {
711 : 24 : blob->md_ro = true;
712 : : }
713 : :
714 [ + + ]: 6948 : if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
715 : 1144 : blob->data_ro = true;
716 : 1144 : blob->md_ro = true;
717 : : }
718 : :
719 : 6948 : blob->invalid_flags = desc_flags->invalid_flags;
720 : 6948 : blob->data_ro_flags = desc_flags->data_ro_flags;
721 : 6948 : blob->md_ro_flags = desc_flags->md_ro_flags;
722 : :
723 [ + + ]: 11281 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
724 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
725 : : unsigned int i, j;
726 : 2784 : unsigned int cluster_count = blob->active.num_clusters;
727 : :
728 [ - + ]: 2784 : if (blob->extent_table_found) {
729 : : /* Extent Table already present in the md,
730 : : * both descriptors should never be at the same time. */
731 : 0 : return -EINVAL;
732 : : }
733 : 2784 : blob->extent_rle_found = true;
734 : :
735 : 2784 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
736 : :
737 [ + - ]: 2784 : if (desc_extent_rle->length == 0 ||
738 [ - + ]: 2784 : (desc_extent_rle->length % sizeof(desc_extent_rle->extents[0]) != 0)) {
739 : 0 : return -EINVAL;
740 : : }
741 : :
742 [ + + ]: 5924 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
743 [ + + ]: 42476 : for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
744 [ + + ]: 39336 : if (desc_extent_rle->extents[i].cluster_idx != 0) {
745 [ - + ]: 13384 : if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters,
746 : 13384 : desc_extent_rle->extents[i].cluster_idx + j)) {
747 : 0 : return -EINVAL;
748 : : }
749 : : }
750 : 39336 : cluster_count++;
751 : : }
752 : : }
753 : :
754 [ - + ]: 2784 : if (cluster_count == 0) {
755 : 0 : return -EINVAL;
756 : : }
757 : 2784 : tmp = realloc(blob->active.clusters, cluster_count * sizeof(*blob->active.clusters));
758 [ - + ]: 2784 : if (tmp == NULL) {
759 : 0 : return -ENOMEM;
760 : : }
761 : 2784 : blob->active.clusters = tmp;
762 : 2784 : blob->active.cluster_array_size = cluster_count;
763 : :
764 [ + + ]: 5924 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
765 [ + + ]: 42476 : for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
766 [ + + ]: 39336 : if (desc_extent_rle->extents[i].cluster_idx != 0) {
767 : 26768 : blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
768 : 13384 : desc_extent_rle->extents[i].cluster_idx + j);
769 : 13384 : blob->active.num_allocated_clusters++;
770 [ + - ]: 25952 : } else if (spdk_blob_is_thin_provisioned(blob)) {
771 : 25952 : blob->active.clusters[blob->active.num_clusters++] = 0;
772 : : } else {
773 : 0 : return -EINVAL;
774 : : }
775 : : }
776 : : }
777 [ + + ]: 8497 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
778 : : struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
779 : 3624 : uint32_t num_extent_pages = blob->active.num_extent_pages;
780 : : uint32_t i, j;
781 : : size_t extent_pages_length;
782 : :
783 : 3624 : desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
784 : 3624 : extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
785 : :
786 [ - + ]: 3624 : if (blob->extent_rle_found) {
787 : : /* This means that Extent RLE is present in MD,
788 : : * both should never be at the same time. */
789 : 0 : return -EINVAL;
790 [ - + ]: 3624 : } else if (blob->extent_table_found &&
791 [ # # ]: 0 : desc_extent_table->num_clusters != blob->remaining_clusters_in_et) {
792 : : /* Number of clusters in this ET does not match number
793 : : * from previously read EXTENT_TABLE. */
794 : 0 : return -EINVAL;
795 : : }
796 : :
797 [ + - ]: 3624 : if (desc_extent_table->length == 0 ||
798 [ - + ]: 3624 : (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
799 : 0 : return -EINVAL;
800 : : }
801 : :
802 : 3624 : blob->extent_table_found = true;
803 : :
804 [ + + ]: 6639 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
805 : 3015 : num_extent_pages += desc_extent_table->extent_page[i].num_pages;
806 : : }
807 : :
808 [ + + ]: 3624 : if (num_extent_pages > 0) {
809 : 2983 : tmp = realloc(blob->active.extent_pages, num_extent_pages * sizeof(uint32_t));
810 [ - + ]: 2983 : if (tmp == NULL) {
811 : 0 : return -ENOMEM;
812 : : }
813 : 2983 : blob->active.extent_pages = tmp;
814 : : }
815 : 3624 : blob->active.extent_pages_array_size = num_extent_pages;
816 : :
817 : 3624 : blob->remaining_clusters_in_et = desc_extent_table->num_clusters;
818 : :
819 : : /* Extent table entries contain md page numbers for extent pages.
820 : : * Zeroes represent unallocated extent pages, those are run-length-encoded.
821 : : */
822 [ + + ]: 6639 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
823 [ + + ]: 3015 : if (desc_extent_table->extent_page[i].page_idx != 0) {
824 [ - + ]: 2137 : assert(desc_extent_table->extent_page[i].num_pages == 1);
825 : 2137 : blob->active.extent_pages[blob->active.num_extent_pages++] =
826 : 2137 : desc_extent_table->extent_page[i].page_idx;
827 [ + - ]: 878 : } else if (spdk_blob_is_thin_provisioned(blob)) {
828 [ + + ]: 1756 : for (j = 0; j < desc_extent_table->extent_page[i].num_pages; j++) {
829 : 878 : blob->active.extent_pages[blob->active.num_extent_pages++] = 0;
830 : : }
831 : : } else {
832 : 0 : return -EINVAL;
833 : : }
834 : : }
835 [ + + ]: 4873 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
836 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
837 : : unsigned int i;
838 : 2125 : unsigned int cluster_count = 0;
839 : : size_t cluster_idx_length;
840 : :
841 [ - + ]: 2125 : if (blob->extent_rle_found) {
842 : : /* This means that Extent RLE is present in MD,
843 : : * both should never be at the same time. */
844 : 0 : return -EINVAL;
845 : : }
846 : :
847 : 2125 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
848 : 2125 : cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
849 : :
850 [ + - ]: 2125 : if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
851 [ - + ]: 2125 : (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
852 : 0 : return -EINVAL;
853 : : }
854 : :
855 [ + + ]: 33484 : for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
856 [ + + ]: 31359 : if (desc_extent->cluster_idx[i] != 0) {
857 [ - + ]: 14651 : if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters, desc_extent->cluster_idx[i])) {
858 : 0 : return -EINVAL;
859 : : }
860 : : }
861 : 31359 : cluster_count++;
862 : : }
863 : :
864 [ - + ]: 2125 : if (cluster_count == 0) {
865 : 0 : return -EINVAL;
866 : : }
867 : :
868 : : /* When reading extent pages sequentially starting cluster idx should match
869 : : * current size of a blob.
870 : : * If changed to batch reading, this check shall be removed. */
871 [ - + ]: 2125 : if (desc_extent->start_cluster_idx != blob->active.num_clusters) {
872 : 0 : return -EINVAL;
873 : : }
874 : :
875 : 2125 : tmp = realloc(blob->active.clusters,
876 : 2125 : (cluster_count + blob->active.num_clusters) * sizeof(*blob->active.clusters));
877 [ - + ]: 2125 : if (tmp == NULL) {
878 : 0 : return -ENOMEM;
879 : : }
880 : 2125 : blob->active.clusters = tmp;
881 : 2125 : blob->active.cluster_array_size = (cluster_count + blob->active.num_clusters);
882 : :
883 [ + + ]: 33484 : for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
884 [ + + ]: 31359 : if (desc_extent->cluster_idx[i] != 0) {
885 : 14651 : blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
886 : : desc_extent->cluster_idx[i]);
887 : 14651 : blob->active.num_allocated_clusters++;
888 [ + - ]: 16708 : } else if (spdk_blob_is_thin_provisioned(blob)) {
889 : 16708 : blob->active.clusters[blob->active.num_clusters++] = 0;
890 : : } else {
891 : 0 : return -EINVAL;
892 : : }
893 : : }
894 [ - + ]: 2125 : assert(desc_extent->start_cluster_idx + cluster_count == blob->active.num_clusters);
895 [ - + ]: 2125 : assert(blob->remaining_clusters_in_et >= cluster_count);
896 : 2125 : blob->remaining_clusters_in_et -= cluster_count;
897 [ + + ]: 2748 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
898 : : int rc;
899 : :
900 : 962 : rc = blob_deserialize_xattr(blob,
901 : : (struct spdk_blob_md_descriptor_xattr *) desc, false);
902 [ - + ]: 962 : if (rc != 0) {
903 : 0 : return rc;
904 : : }
905 [ + - ]: 1786 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
906 : : int rc;
907 : :
908 : 1786 : rc = blob_deserialize_xattr(blob,
909 : : (struct spdk_blob_md_descriptor_xattr *) desc, true);
910 [ - + ]: 1786 : if (rc != 0) {
911 : 0 : return rc;
912 : : }
913 : : } else {
914 : : /* Unrecognized descriptor type. Do not fail - just continue to the
915 : : * next descriptor. If this descriptor is associated with some feature
916 : : * defined in a newer version of blobstore, that version of blobstore
917 : : * should create and set an associated feature flag to specify if this
918 : : * blob can be loaded or not.
919 : : */
920 : : }
921 : :
922 : : /* Advance to the next descriptor */
923 : 18229 : cur_desc += sizeof(*desc) + desc->length;
924 [ + + ]: 18229 : if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
925 : 80 : break;
926 : : }
927 : 18149 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
928 : : }
929 : :
930 : 9273 : return 0;
931 : : }
932 : :
933 : : static bool bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page);
934 : :
935 : : static int
936 : 2125 : blob_parse_extent_page(struct spdk_blob_md_page *extent_page, struct spdk_blob *blob)
937 : : {
938 [ - + ]: 2125 : assert(blob != NULL);
939 [ - + ]: 2125 : assert(blob->state == SPDK_BLOB_STATE_LOADING);
940 : :
941 [ - + ]: 2125 : if (bs_load_cur_extent_page_valid(extent_page) == false) {
942 : 0 : return -ENOENT;
943 : : }
944 : :
945 : 2125 : return blob_parse_page(extent_page, blob);
946 : : }
947 : :
948 : : static int
949 : 6972 : blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count,
950 : : struct spdk_blob *blob)
951 : : {
952 : : const struct spdk_blob_md_page *page;
953 : : uint32_t i;
954 : : int rc;
955 : : void *tmp;
956 : :
957 [ - + ]: 6972 : assert(page_count > 0);
958 [ - + ]: 6972 : assert(pages[0].sequence_num == 0);
959 [ - + ]: 6972 : assert(blob != NULL);
960 [ - + ]: 6972 : assert(blob->state == SPDK_BLOB_STATE_LOADING);
961 [ - + ]: 6972 : assert(blob->active.clusters == NULL);
962 : :
963 : : /* The blobid provided doesn't match what's in the MD, this can
964 : : * happen for example if a bogus blobid is passed in through open.
965 : : */
966 [ + + ]: 6972 : if (blob->id != pages[0].id) {
967 : 8 : SPDK_ERRLOG("Blobid (0x%" PRIx64 ") doesn't match what's in metadata "
968 : : "(0x%" PRIx64 ")\n", blob->id, pages[0].id);
969 : 8 : return -ENOENT;
970 : : }
971 : :
972 : 6964 : tmp = realloc(blob->active.pages, page_count * sizeof(*blob->active.pages));
973 [ - + ]: 6964 : if (!tmp) {
974 : 0 : return -ENOMEM;
975 : : }
976 : 6964 : blob->active.pages = tmp;
977 : :
978 : 6964 : blob->active.pages[0] = pages[0].id;
979 : :
980 [ + + ]: 7164 : for (i = 1; i < page_count; i++) {
981 [ - + ]: 200 : assert(spdk_bit_array_get(blob->bs->used_md_pages, pages[i - 1].next));
982 : 200 : blob->active.pages[i] = pages[i - 1].next;
983 : : }
984 : 6964 : blob->active.num_pages = page_count;
985 : :
986 [ + + ]: 14112 : for (i = 0; i < page_count; i++) {
987 : 7164 : page = &pages[i];
988 : :
989 [ - + ]: 7164 : assert(page->id == blob->id);
990 [ - + ]: 7164 : assert(page->sequence_num == i);
991 : :
992 : 7164 : rc = blob_parse_page(page, blob);
993 [ + + ]: 7164 : if (rc != 0) {
994 : 16 : return rc;
995 : : }
996 : : }
997 : :
998 : 6948 : return 0;
999 : : }
1000 : :
1001 : : static int
1002 : 8748 : blob_serialize_add_page(const struct spdk_blob *blob,
1003 : : struct spdk_blob_md_page **pages,
1004 : : uint32_t *page_count,
1005 : : struct spdk_blob_md_page **last_page)
1006 : : {
1007 : : struct spdk_blob_md_page *page, *tmp_pages;
1008 : :
1009 [ - + ]: 8748 : assert(pages != NULL);
1010 [ - + ]: 8748 : assert(page_count != NULL);
1011 : :
1012 : 8748 : *last_page = NULL;
1013 [ + + ]: 8748 : if (*page_count == 0) {
1014 [ - + ]: 8572 : assert(*pages == NULL);
1015 : 8572 : *pages = spdk_malloc(SPDK_BS_PAGE_SIZE, 0,
1016 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
1017 [ - + ]: 8572 : if (*pages == NULL) {
1018 : 0 : return -ENOMEM;
1019 : : }
1020 : 8572 : *page_count = 1;
1021 : : } else {
1022 [ - + ]: 176 : assert(*pages != NULL);
1023 : 176 : tmp_pages = spdk_realloc(*pages, SPDK_BS_PAGE_SIZE * (*page_count + 1), 0);
1024 [ - + ]: 176 : if (tmp_pages == NULL) {
1025 : 0 : return -ENOMEM;
1026 : : }
1027 : 176 : (*page_count)++;
1028 : 176 : *pages = tmp_pages;
1029 : : }
1030 : :
1031 : 8748 : page = &(*pages)[*page_count - 1];
1032 : 8748 : memset(page, 0, sizeof(*page));
1033 : 8748 : page->id = blob->id;
1034 : 8748 : page->sequence_num = *page_count - 1;
1035 : 8748 : page->next = SPDK_INVALID_MD_PAGE;
1036 : 8748 : *last_page = page;
1037 : :
1038 : 8748 : return 0;
1039 : : }
1040 : :
1041 : : /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor.
1042 : : * Update required_sz on both success and failure.
1043 : : *
1044 : : */
1045 : : static int
1046 : 3614 : blob_serialize_xattr(const struct spdk_xattr *xattr,
1047 : : uint8_t *buf, size_t buf_sz,
1048 : : size_t *required_sz, bool internal)
1049 : : {
1050 : : struct spdk_blob_md_descriptor_xattr *desc;
1051 : :
1052 : 3614 : *required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) +
1053 : 3614 : strlen(xattr->name) +
1054 : 3614 : xattr->value_len;
1055 : :
1056 [ + + ]: 3614 : if (buf_sz < *required_sz) {
1057 : 96 : return -1;
1058 : : }
1059 : :
1060 : 3518 : desc = (struct spdk_blob_md_descriptor_xattr *)buf;
1061 : :
1062 [ + + ]: 3518 : desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR;
1063 : 3518 : desc->length = sizeof(desc->name_length) +
1064 : : sizeof(desc->value_length) +
1065 : 3518 : strlen(xattr->name) +
1066 : 3518 : xattr->value_len;
1067 : 3518 : desc->name_length = strlen(xattr->name);
1068 : 3518 : desc->value_length = xattr->value_len;
1069 : :
1070 : 3518 : memcpy(desc->name, xattr->name, desc->name_length);
1071 : 3518 : memcpy((void *)((uintptr_t)desc->name + desc->name_length),
1072 : 3518 : xattr->value,
1073 : 3518 : desc->value_length);
1074 : :
1075 : 3518 : return 0;
1076 : : }
1077 : :
1078 : : static void
1079 : 3411 : blob_serialize_extent_table_entry(const struct spdk_blob *blob,
1080 : : uint64_t start_ep, uint64_t *next_ep,
1081 : : uint8_t **buf, size_t *remaining_sz)
1082 : : {
1083 : : struct spdk_blob_md_descriptor_extent_table *desc;
1084 : : size_t cur_sz;
1085 : : uint64_t i, et_idx;
1086 : : uint32_t extent_page, ep_len;
1087 : :
1088 : : /* The buffer must have room for at least num_clusters entry */
1089 : 3411 : cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->num_clusters);
1090 [ + + ]: 3411 : if (*remaining_sz < cur_sz) {
1091 : 40 : *next_ep = start_ep;
1092 : 40 : return;
1093 : : }
1094 : :
1095 : 3371 : desc = (struct spdk_blob_md_descriptor_extent_table *)*buf;
1096 : 3371 : desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE;
1097 : :
1098 : 3371 : desc->num_clusters = blob->active.num_clusters;
1099 : :
1100 : 3371 : ep_len = 1;
1101 : 3371 : et_idx = 0;
1102 [ + + ]: 8536 : for (i = start_ep; i < blob->active.num_extent_pages; i++) {
1103 [ - + ]: 5165 : if (*remaining_sz < cur_sz + sizeof(desc->extent_page[0])) {
1104 : : /* If we ran out of buffer space, return */
1105 : 0 : break;
1106 : : }
1107 : :
1108 : 5165 : extent_page = blob->active.extent_pages[i];
1109 : : /* Verify that next extent_page is unallocated */
1110 [ + + ]: 5165 : if (extent_page == 0 &&
1111 [ + + + - ]: 3046 : (i + 1 < blob->active.num_extent_pages && blob->active.extent_pages[i + 1] == 0)) {
1112 : 2156 : ep_len++;
1113 : 2156 : continue;
1114 : : }
1115 : 3009 : desc->extent_page[et_idx].page_idx = extent_page;
1116 : 3009 : desc->extent_page[et_idx].num_pages = ep_len;
1117 : 3009 : et_idx++;
1118 : :
1119 : 3009 : ep_len = 1;
1120 : 3009 : cur_sz += sizeof(desc->extent_page[et_idx]);
1121 : : }
1122 : 3371 : *next_ep = i;
1123 : :
1124 : 3371 : desc->length = sizeof(desc->num_clusters) + sizeof(desc->extent_page[0]) * et_idx;
1125 : 3371 : *remaining_sz -= sizeof(struct spdk_blob_md_descriptor) + desc->length;
1126 : 3371 : *buf += sizeof(struct spdk_blob_md_descriptor) + desc->length;
1127 : : }
1128 : :
1129 : : static int
1130 : 3375 : blob_serialize_extent_table(const struct spdk_blob *blob,
1131 : : struct spdk_blob_md_page **pages,
1132 : : struct spdk_blob_md_page *cur_page,
1133 : : uint32_t *page_count, uint8_t **buf,
1134 : : size_t *remaining_sz)
1135 : : {
1136 : 3363 : uint64_t last_extent_page;
1137 : : int rc;
1138 : :
1139 : 3375 : last_extent_page = 0;
1140 : : /* At least single extent table entry has to be always persisted.
1141 : : * Such case occurs with num_extent_pages == 0. */
1142 [ + - ]: 3411 : while (last_extent_page <= blob->active.num_extent_pages) {
1143 : 3411 : blob_serialize_extent_table_entry(blob, last_extent_page, &last_extent_page, buf,
1144 : : remaining_sz);
1145 : :
1146 [ + + ]: 3411 : if (last_extent_page == blob->active.num_extent_pages) {
1147 : 3375 : break;
1148 : : }
1149 : :
1150 : 36 : rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
1151 [ - + ]: 36 : if (rc < 0) {
1152 : 0 : return rc;
1153 : : }
1154 : :
1155 : 36 : *buf = (uint8_t *)cur_page->descriptors;
1156 : 36 : *remaining_sz = sizeof(cur_page->descriptors);
1157 : : }
1158 : :
1159 : 3375 : return 0;
1160 : : }
1161 : :
1162 : : static void
1163 : 3474 : blob_serialize_extent_rle(const struct spdk_blob *blob,
1164 : : uint64_t start_cluster, uint64_t *next_cluster,
1165 : : uint8_t **buf, size_t *buf_sz)
1166 : : {
1167 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
1168 : : size_t cur_sz;
1169 : : uint64_t i, extent_idx;
1170 : : uint64_t lba, lba_per_cluster, lba_count;
1171 : :
1172 : : /* The buffer must have room for at least one extent */
1173 : 3474 : cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc_extent_rle->extents[0]);
1174 [ + + ]: 3474 : if (*buf_sz < cur_sz) {
1175 : 36 : *next_cluster = start_cluster;
1176 : 36 : return;
1177 : : }
1178 : :
1179 : 3438 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)*buf;
1180 : 3438 : desc_extent_rle->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE;
1181 : :
1182 : 3438 : lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
1183 : : /* Assert for scan-build false positive */
1184 [ - + ]: 3438 : assert(lba_per_cluster > 0);
1185 : :
1186 : 3438 : lba = blob->active.clusters[start_cluster];
1187 : 3438 : lba_count = lba_per_cluster;
1188 : 3438 : extent_idx = 0;
1189 [ + + ]: 1620900 : for (i = start_cluster + 1; i < blob->active.num_clusters; i++) {
1190 [ + + + + ]: 1617470 : if ((lba + lba_count) == blob->active.clusters[i] && lba != 0) {
1191 : : /* Run-length encode sequential non-zero LBA */
1192 : 14552 : lba_count += lba_per_cluster;
1193 : 14552 : continue;
1194 [ + + + + ]: 1602918 : } else if (lba == 0 && blob->active.clusters[i] == 0) {
1195 : : /* Run-length encode unallocated clusters */
1196 : 1600532 : lba_count += lba_per_cluster;
1197 : 1600532 : continue;
1198 : : }
1199 : 2386 : desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
1200 : 2386 : desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
1201 : 2386 : extent_idx++;
1202 : :
1203 : 2386 : cur_sz += sizeof(desc_extent_rle->extents[extent_idx]);
1204 : :
1205 [ + + ]: 2386 : if (*buf_sz < cur_sz) {
1206 : : /* If we ran out of buffer space, return */
1207 : 8 : *next_cluster = i;
1208 : 8 : break;
1209 : : }
1210 : :
1211 : 2378 : lba = blob->active.clusters[i];
1212 : 2378 : lba_count = lba_per_cluster;
1213 : : }
1214 : :
1215 [ + + ]: 3438 : if (*buf_sz >= cur_sz) {
1216 : 3430 : desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
1217 : 3430 : desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
1218 : 3430 : extent_idx++;
1219 : :
1220 : 3430 : *next_cluster = blob->active.num_clusters;
1221 : : }
1222 : :
1223 : 3438 : desc_extent_rle->length = sizeof(desc_extent_rle->extents[0]) * extent_idx;
1224 : 3438 : *buf_sz -= sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
1225 : 3438 : *buf += sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
1226 : : }
1227 : :
1228 : : static int
1229 : 3858 : blob_serialize_extents_rle(const struct spdk_blob *blob,
1230 : : struct spdk_blob_md_page **pages,
1231 : : struct spdk_blob_md_page *cur_page,
1232 : : uint32_t *page_count, uint8_t **buf,
1233 : : size_t *remaining_sz)
1234 : : {
1235 : 3858 : uint64_t last_cluster;
1236 : : int rc;
1237 : :
1238 : 3858 : last_cluster = 0;
1239 [ + + ]: 3902 : while (last_cluster < blob->active.num_clusters) {
1240 : 3474 : blob_serialize_extent_rle(blob, last_cluster, &last_cluster, buf, remaining_sz);
1241 : :
1242 [ + + ]: 3474 : if (last_cluster == blob->active.num_clusters) {
1243 : 3430 : break;
1244 : : }
1245 : :
1246 : 44 : rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
1247 [ - + ]: 44 : if (rc < 0) {
1248 : 0 : return rc;
1249 : : }
1250 : :
1251 : 44 : *buf = (uint8_t *)cur_page->descriptors;
1252 : 44 : *remaining_sz = sizeof(cur_page->descriptors);
1253 : : }
1254 : :
1255 : 3858 : return 0;
1256 : : }
1257 : :
1258 : : static void
1259 : 2213 : blob_serialize_extent_page(const struct spdk_blob *blob,
1260 : : uint64_t cluster, struct spdk_blob_md_page *page)
1261 : : {
1262 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
1263 : : uint64_t i, extent_idx;
1264 : : uint64_t lba, lba_per_cluster;
1265 : 2213 : uint64_t start_cluster_idx = (cluster / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
1266 : :
1267 : 2213 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *) page->descriptors;
1268 : 2213 : desc_extent->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE;
1269 : :
1270 : 2213 : lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
1271 : :
1272 : 2213 : desc_extent->start_cluster_idx = start_cluster_idx;
1273 : 2213 : extent_idx = 0;
1274 [ + + ]: 84948 : for (i = start_cluster_idx; i < blob->active.num_clusters; i++) {
1275 : 82867 : lba = blob->active.clusters[i];
1276 : 82867 : desc_extent->cluster_idx[extent_idx++] = lba / lba_per_cluster;
1277 [ + + ]: 82867 : if (extent_idx >= SPDK_EXTENTS_PER_EP) {
1278 : 132 : break;
1279 : : }
1280 : : }
1281 : 2213 : desc_extent->length = sizeof(desc_extent->start_cluster_idx) +
1282 : : sizeof(desc_extent->cluster_idx[0]) * extent_idx;
1283 : 2213 : }
1284 : :
1285 : : static void
1286 : 7233 : blob_serialize_flags(const struct spdk_blob *blob,
1287 : : uint8_t *buf, size_t *buf_sz)
1288 : : {
1289 : : struct spdk_blob_md_descriptor_flags *desc;
1290 : :
1291 : : /*
1292 : : * Flags get serialized first, so we should always have room for the flags
1293 : : * descriptor.
1294 : : */
1295 [ - + ]: 7233 : assert(*buf_sz >= sizeof(*desc));
1296 : :
1297 : 7233 : desc = (struct spdk_blob_md_descriptor_flags *)buf;
1298 : 7233 : desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS;
1299 : 7233 : desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor);
1300 : 7233 : desc->invalid_flags = blob->invalid_flags;
1301 : 7233 : desc->data_ro_flags = blob->data_ro_flags;
1302 : 7233 : desc->md_ro_flags = blob->md_ro_flags;
1303 : :
1304 : 7233 : *buf_sz -= sizeof(*desc);
1305 : 7233 : }
1306 : :
1307 : : static int
1308 : 14466 : blob_serialize_xattrs(const struct spdk_blob *blob,
1309 : : const struct spdk_xattr_tailq *xattrs, bool internal,
1310 : : struct spdk_blob_md_page **pages,
1311 : : struct spdk_blob_md_page *cur_page,
1312 : : uint32_t *page_count, uint8_t **buf,
1313 : : size_t *remaining_sz)
1314 : : {
1315 : : const struct spdk_xattr *xattr;
1316 : : int rc;
1317 : :
1318 [ + + ]: 17984 : TAILQ_FOREACH(xattr, xattrs, link) {
1319 : 3518 : size_t required_sz = 0;
1320 : :
1321 : 3518 : rc = blob_serialize_xattr(xattr,
1322 : : *buf, *remaining_sz,
1323 : : &required_sz, internal);
1324 [ + + ]: 3518 : if (rc < 0) {
1325 : : /* Need to add a new page to the chain */
1326 : 96 : rc = blob_serialize_add_page(blob, pages, page_count,
1327 : : &cur_page);
1328 [ - + ]: 96 : if (rc < 0) {
1329 : 0 : spdk_free(*pages);
1330 : 0 : *pages = NULL;
1331 : 0 : *page_count = 0;
1332 : 0 : return rc;
1333 : : }
1334 : :
1335 : 96 : *buf = (uint8_t *)cur_page->descriptors;
1336 : 96 : *remaining_sz = sizeof(cur_page->descriptors);
1337 : :
1338 : : /* Try again */
1339 : 96 : required_sz = 0;
1340 : 96 : rc = blob_serialize_xattr(xattr,
1341 : : *buf, *remaining_sz,
1342 : : &required_sz, internal);
1343 : :
1344 [ - + ]: 96 : if (rc < 0) {
1345 : 0 : spdk_free(*pages);
1346 : 0 : *pages = NULL;
1347 : 0 : *page_count = 0;
1348 : 0 : return rc;
1349 : : }
1350 : : }
1351 : :
1352 : 3518 : *remaining_sz -= required_sz;
1353 : 3518 : *buf += required_sz;
1354 : : }
1355 : :
1356 : 14466 : return 0;
1357 : : }
1358 : :
1359 : : static int
1360 : 7233 : blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages,
1361 : : uint32_t *page_count)
1362 : : {
1363 : 7221 : struct spdk_blob_md_page *cur_page;
1364 : : int rc;
1365 : 7221 : uint8_t *buf;
1366 : 7221 : size_t remaining_sz;
1367 : :
1368 [ - + ]: 7233 : assert(pages != NULL);
1369 [ - + ]: 7233 : assert(page_count != NULL);
1370 [ - + ]: 7233 : assert(blob != NULL);
1371 [ - + ]: 7233 : assert(blob->state == SPDK_BLOB_STATE_DIRTY);
1372 : :
1373 : 7233 : *pages = NULL;
1374 : 7233 : *page_count = 0;
1375 : :
1376 : : /* A blob always has at least 1 page, even if it has no descriptors */
1377 : 7233 : rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
1378 [ - + ]: 7233 : if (rc < 0) {
1379 : 0 : return rc;
1380 : : }
1381 : :
1382 : 7233 : buf = (uint8_t *)cur_page->descriptors;
1383 : 7233 : remaining_sz = sizeof(cur_page->descriptors);
1384 : :
1385 : : /* Serialize flags */
1386 : 7233 : blob_serialize_flags(blob, buf, &remaining_sz);
1387 : 7233 : buf += sizeof(struct spdk_blob_md_descriptor_flags);
1388 : :
1389 : : /* Serialize xattrs */
1390 : 7233 : rc = blob_serialize_xattrs(blob, &blob->xattrs, false,
1391 : : pages, cur_page, page_count, &buf, &remaining_sz);
1392 [ - + ]: 7233 : if (rc < 0) {
1393 : 0 : return rc;
1394 : : }
1395 : :
1396 : : /* Serialize internal xattrs */
1397 : 7233 : rc = blob_serialize_xattrs(blob, &blob->xattrs_internal, true,
1398 : : pages, cur_page, page_count, &buf, &remaining_sz);
1399 [ - + ]: 7233 : if (rc < 0) {
1400 : 0 : return rc;
1401 : : }
1402 : :
1403 [ + + ]: 7233 : if (blob->use_extent_table) {
1404 : : /* Serialize extent table */
1405 : 3375 : rc = blob_serialize_extent_table(blob, pages, cur_page, page_count, &buf, &remaining_sz);
1406 : : } else {
1407 : : /* Serialize extents */
1408 : 3858 : rc = blob_serialize_extents_rle(blob, pages, cur_page, page_count, &buf, &remaining_sz);
1409 : : }
1410 : :
1411 : 7233 : return rc;
1412 : : }
1413 : :
1414 : : struct spdk_blob_load_ctx {
1415 : : struct spdk_blob *blob;
1416 : :
1417 : : struct spdk_blob_md_page *pages;
1418 : : uint32_t num_pages;
1419 : : uint32_t next_extent_page;
1420 : : spdk_bs_sequence_t *seq;
1421 : :
1422 : : spdk_bs_sequence_cpl cb_fn;
1423 : : void *cb_arg;
1424 : : };
1425 : :
1426 : : static uint32_t
1427 : 40143 : blob_md_page_calc_crc(void *page)
1428 : : {
1429 : : uint32_t crc;
1430 : :
1431 : 40143 : crc = BLOB_CRC32C_INITIAL;
1432 : 40143 : crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc);
1433 : 40143 : crc ^= BLOB_CRC32C_INITIAL;
1434 : :
1435 : 40143 : return crc;
1436 : :
1437 : : }
1438 : :
1439 : : static void
1440 : 7028 : blob_load_final(struct spdk_blob_load_ctx *ctx, int bserrno)
1441 : : {
1442 : 7028 : struct spdk_blob *blob = ctx->blob;
1443 : :
1444 [ + + ]: 7028 : if (bserrno == 0) {
1445 : 6900 : blob_mark_clean(blob);
1446 : : }
1447 : :
1448 : 7028 : ctx->cb_fn(ctx->seq, ctx->cb_arg, bserrno);
1449 : :
1450 : : /* Free the memory */
1451 : 7028 : spdk_free(ctx->pages);
1452 : 7028 : free(ctx);
1453 : 7028 : }
1454 : :
1455 : : static void
1456 : 928 : blob_load_snapshot_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
1457 : : {
1458 : 928 : struct spdk_blob_load_ctx *ctx = cb_arg;
1459 : 928 : struct spdk_blob *blob = ctx->blob;
1460 : :
1461 [ + + ]: 928 : if (bserrno == 0) {
1462 : 916 : blob->back_bs_dev = bs_create_blob_bs_dev(snapshot);
1463 [ - + ]: 916 : if (blob->back_bs_dev == NULL) {
1464 : 0 : bserrno = -ENOMEM;
1465 : : }
1466 : : }
1467 [ + + ]: 928 : if (bserrno != 0) {
1468 : 12 : SPDK_ERRLOG("Snapshot fail\n");
1469 : : }
1470 : :
1471 : 928 : blob_load_final(ctx, bserrno);
1472 : 928 : }
1473 : :
1474 : : static void blob_update_clear_method(struct spdk_blob *blob);
1475 : :
1476 : : static int
1477 : 240 : blob_load_esnap(struct spdk_blob *blob, void *blob_ctx)
1478 : : {
1479 : 240 : struct spdk_blob_store *bs = blob->bs;
1480 : 240 : struct spdk_bs_dev *bs_dev = NULL;
1481 : 240 : const void *esnap_id = NULL;
1482 : 240 : size_t id_len = 0;
1483 : : int rc;
1484 : :
1485 [ + + ]: 240 : if (bs->esnap_bs_dev_create == NULL) {
1486 : 16 : SPDK_NOTICELOG("blob 0x%" PRIx64 " is an esnap clone but the blobstore was opened "
1487 : : "without support for esnap clones\n", blob->id);
1488 : 16 : return -ENOTSUP;
1489 : : }
1490 [ - + ]: 224 : assert(blob->back_bs_dev == NULL);
1491 : :
1492 : 224 : rc = blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, &esnap_id, &id_len, true);
1493 [ - + ]: 224 : if (rc != 0) {
1494 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " is an esnap clone but has no esnap ID\n", blob->id);
1495 : 0 : return -EINVAL;
1496 : : }
1497 [ + - + + ]: 224 : assert(id_len > 0 && id_len < UINT32_MAX);
1498 : :
1499 [ - + ]: 224 : SPDK_INFOLOG(blob, "Creating external snapshot device\n");
1500 : :
1501 : 224 : rc = bs->esnap_bs_dev_create(bs->esnap_ctx, blob_ctx, blob, esnap_id, (uint32_t)id_len,
1502 : : &bs_dev);
1503 [ - + ]: 224 : if (rc != 0) {
1504 [ # # ]: 0 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": failed to load back_bs_dev "
1505 : : "with error %d\n", blob->id, rc);
1506 : 0 : return rc;
1507 : : }
1508 : :
1509 : : /*
1510 : : * Note: bs_dev might be NULL if the consumer chose to not open the external snapshot.
1511 : : * This especially might happen during spdk_bs_load() iteration.
1512 : : */
1513 [ + - ]: 224 : if (bs_dev != NULL) {
1514 [ - + ]: 224 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": loaded back_bs_dev\n", blob->id);
1515 [ + + ]: 224 : if ((bs->io_unit_size % bs_dev->blocklen) != 0) {
1516 : 8 : SPDK_NOTICELOG("blob 0x%" PRIx64 " external snapshot device block size %u "
1517 : : "is not compatible with blobstore block size %u\n",
1518 : : blob->id, bs_dev->blocklen, bs->io_unit_size);
1519 : 8 : bs_dev->destroy(bs_dev);
1520 : 8 : return -EINVAL;
1521 : : }
1522 : : }
1523 : :
1524 : 216 : blob->back_bs_dev = bs_dev;
1525 : 216 : blob->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
1526 : :
1527 : 216 : return 0;
1528 : : }
1529 : :
1530 : : static void
1531 : 6936 : blob_load_backing_dev(spdk_bs_sequence_t *seq, void *cb_arg)
1532 : : {
1533 : 6936 : struct spdk_blob_load_ctx *ctx = cb_arg;
1534 : 6936 : struct spdk_blob *blob = ctx->blob;
1535 : 6928 : const void *value;
1536 : 6928 : size_t len;
1537 : : int rc;
1538 : :
1539 [ + + ]: 6936 : if (blob_is_esnap_clone(blob)) {
1540 : 240 : rc = blob_load_esnap(blob, seq->cpl.u.blob_handle.esnap_ctx);
1541 : 240 : blob_load_final(ctx, rc);
1542 : 240 : return;
1543 : : }
1544 : :
1545 [ + + ]: 6696 : if (spdk_blob_is_thin_provisioned(blob)) {
1546 : 2100 : rc = blob_get_xattr_value(blob, BLOB_SNAPSHOT, &value, &len, true);
1547 [ + + ]: 2100 : if (rc == 0) {
1548 [ - + ]: 928 : if (len != sizeof(spdk_blob_id)) {
1549 : 0 : blob_load_final(ctx, -EINVAL);
1550 : 0 : return;
1551 : : }
1552 : : /* open snapshot blob and continue in the callback function */
1553 : 928 : blob->parent_id = *(spdk_blob_id *)value;
1554 : 928 : spdk_bs_open_blob(blob->bs, blob->parent_id,
1555 : : blob_load_snapshot_cpl, ctx);
1556 : 928 : return;
1557 : : } else {
1558 : : /* add zeroes_dev for thin provisioned blob */
1559 : 1172 : blob->back_bs_dev = bs_create_zeroes_dev();
1560 : : }
1561 : : } else {
1562 : : /* standard blob */
1563 : 4596 : blob->back_bs_dev = NULL;
1564 : : }
1565 : 5768 : blob_load_final(ctx, 0);
1566 : : }
1567 : :
1568 : : static void
1569 : 5761 : blob_load_cpl_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1570 : : {
1571 : 5761 : struct spdk_blob_load_ctx *ctx = cb_arg;
1572 : 5761 : struct spdk_blob *blob = ctx->blob;
1573 : : struct spdk_blob_md_page *page;
1574 : : uint64_t i;
1575 : : uint32_t crc;
1576 : : uint64_t lba;
1577 : : void *tmp;
1578 : : uint64_t sz;
1579 : :
1580 [ + + ]: 5761 : if (bserrno) {
1581 : 12 : SPDK_ERRLOG("Extent page read failed: %d\n", bserrno);
1582 : 12 : blob_load_final(ctx, bserrno);
1583 : 12 : return;
1584 : : }
1585 : :
1586 [ + + ]: 5749 : if (ctx->pages == NULL) {
1587 : : /* First iteration of this function, allocate buffer for single EXTENT_PAGE */
1588 : 3624 : ctx->pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
1589 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
1590 [ - + ]: 3624 : if (!ctx->pages) {
1591 : 0 : blob_load_final(ctx, -ENOMEM);
1592 : 0 : return;
1593 : : }
1594 : 3624 : ctx->num_pages = 1;
1595 : 3624 : ctx->next_extent_page = 0;
1596 : : } else {
1597 : 2125 : page = &ctx->pages[0];
1598 : 2125 : crc = blob_md_page_calc_crc(page);
1599 [ - + ]: 2125 : if (crc != page->crc) {
1600 : 0 : blob_load_final(ctx, -EINVAL);
1601 : 0 : return;
1602 : : }
1603 : :
1604 [ - + ]: 2125 : if (page->next != SPDK_INVALID_MD_PAGE) {
1605 : 0 : blob_load_final(ctx, -EINVAL);
1606 : 0 : return;
1607 : : }
1608 : :
1609 : 2125 : bserrno = blob_parse_extent_page(page, blob);
1610 [ - + ]: 2125 : if (bserrno) {
1611 : 0 : blob_load_final(ctx, bserrno);
1612 : 0 : return;
1613 : : }
1614 : : }
1615 : :
1616 [ + + ]: 6627 : for (i = ctx->next_extent_page; i < blob->active.num_extent_pages; i++) {
1617 [ + + ]: 3015 : if (blob->active.extent_pages[i] != 0) {
1618 : : /* Extent page was allocated, read and parse it. */
1619 : 2137 : lba = bs_md_page_to_lba(blob->bs, blob->active.extent_pages[i]);
1620 : 2137 : ctx->next_extent_page = i + 1;
1621 : :
1622 : 2137 : bs_sequence_read_dev(seq, &ctx->pages[0], lba,
1623 : 2137 : bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
1624 : : blob_load_cpl_extents_cpl, ctx);
1625 : 2137 : return;
1626 : : } else {
1627 : : /* Thin provisioned blobs can point to unallocated extent pages.
1628 : : * In this case blob size should be increased by up to the amount left in remaining_clusters_in_et. */
1629 : :
1630 [ + + ]: 878 : sz = spdk_min(blob->remaining_clusters_in_et, SPDK_EXTENTS_PER_EP);
1631 : 878 : blob->active.num_clusters += sz;
1632 : 878 : blob->remaining_clusters_in_et -= sz;
1633 : :
1634 [ - + ]: 878 : assert(spdk_blob_is_thin_provisioned(blob));
1635 [ + - - + ]: 878 : assert(i + 1 < blob->active.num_extent_pages || blob->remaining_clusters_in_et == 0);
1636 : :
1637 : 878 : tmp = realloc(blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
1638 [ - + ]: 878 : if (tmp == NULL) {
1639 : 0 : blob_load_final(ctx, -ENOMEM);
1640 : 0 : return;
1641 : : }
1642 : 878 : memset(tmp + sizeof(*blob->active.clusters) * blob->active.cluster_array_size, 0,
1643 : 878 : sizeof(*blob->active.clusters) * (blob->active.num_clusters - blob->active.cluster_array_size));
1644 : 878 : blob->active.clusters = tmp;
1645 : 878 : blob->active.cluster_array_size = blob->active.num_clusters;
1646 : : }
1647 : : }
1648 : :
1649 : 3612 : blob_load_backing_dev(seq, ctx);
1650 : : }
1651 : :
1652 : : static void
1653 : 7228 : blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1654 : : {
1655 : 7228 : struct spdk_blob_load_ctx *ctx = cb_arg;
1656 : 7228 : struct spdk_blob *blob = ctx->blob;
1657 : : struct spdk_blob_md_page *page;
1658 : : int rc;
1659 : : uint32_t crc;
1660 : : uint32_t current_page;
1661 : :
1662 [ + + ]: 7228 : if (ctx->num_pages == 1) {
1663 : 7028 : current_page = bs_blobid_to_page(blob->id);
1664 : : } else {
1665 [ - + ]: 200 : assert(ctx->num_pages != 0);
1666 : 200 : page = &ctx->pages[ctx->num_pages - 2];
1667 : 200 : current_page = page->next;
1668 : : }
1669 : :
1670 [ + + ]: 7228 : if (bserrno) {
1671 : 40 : SPDK_ERRLOG("Metadata page %d read failed for blobid 0x%" PRIx64 ": %d\n",
1672 : : current_page, blob->id, bserrno);
1673 : 40 : blob_load_final(ctx, bserrno);
1674 : 40 : return;
1675 : : }
1676 : :
1677 : 7188 : page = &ctx->pages[ctx->num_pages - 1];
1678 : 7188 : crc = blob_md_page_calc_crc(page);
1679 [ + + ]: 7188 : if (crc != page->crc) {
1680 : 16 : SPDK_ERRLOG("Metadata page %d crc mismatch for blobid 0x%" PRIx64 "\n",
1681 : : current_page, blob->id);
1682 : 16 : blob_load_final(ctx, -EINVAL);
1683 : 16 : return;
1684 : : }
1685 : :
1686 [ + + ]: 7172 : if (page->next != SPDK_INVALID_MD_PAGE) {
1687 : : struct spdk_blob_md_page *tmp_pages;
1688 : 200 : uint32_t next_page = page->next;
1689 : 200 : uint64_t next_lba = bs_md_page_to_lba(blob->bs, next_page);
1690 : :
1691 : : /* Read the next page */
1692 : 200 : tmp_pages = spdk_realloc(ctx->pages, (sizeof(*page) * (ctx->num_pages + 1)), 0);
1693 [ - + ]: 200 : if (tmp_pages == NULL) {
1694 : 0 : blob_load_final(ctx, -ENOMEM);
1695 : 0 : return;
1696 : : }
1697 : 200 : ctx->num_pages++;
1698 : 200 : ctx->pages = tmp_pages;
1699 : :
1700 : 200 : bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1],
1701 : : next_lba,
1702 : 200 : bs_byte_to_lba(blob->bs, sizeof(*page)),
1703 : : blob_load_cpl, ctx);
1704 : 200 : return;
1705 : : }
1706 : :
1707 : : /* Parse the pages */
1708 : 6972 : rc = blob_parse(ctx->pages, ctx->num_pages, blob);
1709 [ + + ]: 6972 : if (rc) {
1710 : 24 : blob_load_final(ctx, rc);
1711 : 24 : return;
1712 : : }
1713 : :
1714 [ + + ]: 6948 : if (blob->extent_table_found == true) {
1715 : : /* If EXTENT_TABLE was found, that means support for it should be enabled. */
1716 [ - + ]: 3624 : assert(blob->extent_rle_found == false);
1717 : 3624 : blob->use_extent_table = true;
1718 : : } else {
1719 : : /* If EXTENT_RLE or no extent_* descriptor was found disable support
1720 : : * for extent table. No extent_* descriptors means that blob has length of 0
1721 : : * and no extent_rle descriptors were persisted for it.
1722 : : * EXTENT_TABLE if used, is always present in metadata regardless of length. */
1723 : 3324 : blob->use_extent_table = false;
1724 : : }
1725 : :
1726 : : /* Check the clear_method stored in metadata vs what may have been passed
1727 : : * via spdk_bs_open_blob_ext() and update accordingly.
1728 : : */
1729 : 6948 : blob_update_clear_method(blob);
1730 : :
1731 : 6948 : spdk_free(ctx->pages);
1732 : 6948 : ctx->pages = NULL;
1733 : :
1734 [ + + ]: 6948 : if (blob->extent_table_found) {
1735 : 3624 : blob_load_cpl_extents_cpl(seq, ctx, 0);
1736 : : } else {
1737 : 3324 : blob_load_backing_dev(seq, ctx);
1738 : : }
1739 : : }
1740 : :
1741 : : /* Load a blob from disk given a blobid */
1742 : : static void
1743 : 7028 : blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
1744 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg)
1745 : : {
1746 : : struct spdk_blob_load_ctx *ctx;
1747 : : struct spdk_blob_store *bs;
1748 : : uint32_t page_num;
1749 : : uint64_t lba;
1750 : :
1751 : 7028 : blob_verify_md_op(blob);
1752 : :
1753 : 7028 : bs = blob->bs;
1754 : :
1755 : 7028 : ctx = calloc(1, sizeof(*ctx));
1756 [ - + ]: 7028 : if (!ctx) {
1757 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
1758 : 0 : return;
1759 : : }
1760 : :
1761 : 7028 : ctx->blob = blob;
1762 : 7028 : ctx->pages = spdk_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, 0);
1763 [ - + ]: 7028 : if (!ctx->pages) {
1764 : 0 : free(ctx);
1765 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
1766 : 0 : return;
1767 : : }
1768 : 7028 : ctx->num_pages = 1;
1769 : 7028 : ctx->cb_fn = cb_fn;
1770 : 7028 : ctx->cb_arg = cb_arg;
1771 : 7028 : ctx->seq = seq;
1772 : :
1773 : 7028 : page_num = bs_blobid_to_page(blob->id);
1774 : 7028 : lba = bs_md_page_to_lba(blob->bs, page_num);
1775 : :
1776 : 7028 : blob->state = SPDK_BLOB_STATE_LOADING;
1777 : :
1778 : 7028 : bs_sequence_read_dev(seq, &ctx->pages[0], lba,
1779 : 7028 : bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE),
1780 : : blob_load_cpl, ctx);
1781 : : }
1782 : :
1783 : : struct spdk_blob_persist_ctx {
1784 : : struct spdk_blob *blob;
1785 : :
1786 : : struct spdk_blob_md_page *pages;
1787 : : uint32_t next_extent_page;
1788 : : struct spdk_blob_md_page *extent_page;
1789 : :
1790 : : spdk_bs_sequence_t *seq;
1791 : : spdk_bs_sequence_cpl cb_fn;
1792 : : void *cb_arg;
1793 : : TAILQ_ENTRY(spdk_blob_persist_ctx) link;
1794 : : };
1795 : :
1796 : : static void
1797 : 2526 : bs_batch_clear_dev(struct spdk_blob *blob, spdk_bs_batch_t *batch, uint64_t lba,
1798 : : uint64_t lba_count)
1799 : : {
1800 [ + - - ]: 2526 : switch (blob->clear_method) {
1801 : 2526 : case BLOB_CLEAR_WITH_DEFAULT:
1802 : : case BLOB_CLEAR_WITH_UNMAP:
1803 : 2526 : bs_batch_unmap_dev(batch, lba, lba_count);
1804 : 2526 : break;
1805 : 0 : case BLOB_CLEAR_WITH_WRITE_ZEROES:
1806 : 0 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
1807 : 0 : break;
1808 : 0 : case BLOB_CLEAR_WITH_NONE:
1809 : : default:
1810 : 0 : break;
1811 : : }
1812 : 2526 : }
1813 : :
1814 : : static int
1815 : 4074 : bs_super_validate(struct spdk_bs_super_block *super, struct spdk_blob_store *bs)
1816 : : {
1817 : : uint32_t crc;
1818 : : static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
1819 : :
1820 [ + + ]: 4074 : if (super->version > SPDK_BS_VERSION ||
1821 [ + + ]: 3949 : super->version < SPDK_BS_INITIAL_VERSION) {
1822 : 1643 : return -EILSEQ;
1823 : : }
1824 : :
1825 [ + + ]: 2431 : if (memcmp(super->signature, SPDK_BS_SUPER_BLOCK_SIG,
1826 : : sizeof(super->signature)) != 0) {
1827 : 111 : return -EILSEQ;
1828 : : }
1829 : :
1830 : 2320 : crc = blob_md_page_calc_crc(super);
1831 [ + + ]: 2320 : if (crc != super->crc) {
1832 : 8 : return -EILSEQ;
1833 : : }
1834 : :
1835 [ + + ]: 2312 : if (memcmp(&bs->bstype, &super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
1836 [ - + ]: 2284 : SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n");
1837 [ + + ]: 28 : } else if (memcmp(&bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
1838 [ - + ]: 12 : SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n");
1839 : : } else {
1840 [ - + ]: 16 : SPDK_DEBUGLOG(blob, "Unexpected bstype\n");
1841 [ - + ]: 16 : SPDK_LOGDUMP(blob, "Expected:", bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1842 [ - + ]: 16 : SPDK_LOGDUMP(blob, "Found:", super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1843 : 16 : return -ENXIO;
1844 : : }
1845 : :
1846 [ + + ]: 2296 : if (super->size > bs->dev->blockcnt * bs->dev->blocklen) {
1847 : 16 : SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n",
1848 : : bs->dev->blockcnt * bs->dev->blocklen, super->size);
1849 : 16 : return -EILSEQ;
1850 : : }
1851 : :
1852 : 2280 : return 0;
1853 : : }
1854 : :
1855 : : static void bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
1856 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg);
1857 : :
1858 : : static void
1859 : 10235 : blob_persist_complete_cb(void *arg)
1860 : : {
1861 : 10235 : struct spdk_blob_persist_ctx *ctx = arg;
1862 : :
1863 : : /* Call user callback */
1864 : 10235 : ctx->cb_fn(ctx->seq, ctx->cb_arg, 0);
1865 : :
1866 : : /* Free the memory */
1867 : 10235 : spdk_free(ctx->pages);
1868 : 10235 : free(ctx);
1869 : 10235 : }
1870 : :
1871 : : static void blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
1872 : :
1873 : : static void
1874 : 10235 : blob_persist_complete(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx, int bserrno)
1875 : : {
1876 : : struct spdk_blob_persist_ctx *next_persist, *tmp;
1877 : 10235 : struct spdk_blob *blob = ctx->blob;
1878 : :
1879 [ + + ]: 10235 : if (bserrno == 0) {
1880 : 10131 : blob_mark_clean(blob);
1881 : : }
1882 : :
1883 [ - + ]: 10235 : assert(ctx == TAILQ_FIRST(&blob->persists_to_complete));
1884 : :
1885 : : /* Complete all persists that were pending when the current persist started */
1886 [ + + ]: 20470 : TAILQ_FOREACH_SAFE(next_persist, &blob->persists_to_complete, link, tmp) {
1887 [ - + ]: 10235 : TAILQ_REMOVE(&blob->persists_to_complete, next_persist, link);
1888 : 10235 : spdk_thread_send_msg(spdk_get_thread(), blob_persist_complete_cb, next_persist);
1889 : : }
1890 : :
1891 [ + + ]: 10235 : if (TAILQ_EMPTY(&blob->pending_persists)) {
1892 : 10189 : return;
1893 : : }
1894 : :
1895 : : /* Queue up all pending persists for completion and start blob persist with first one */
1896 [ + - - + ]: 46 : TAILQ_SWAP(&blob->persists_to_complete, &blob->pending_persists, spdk_blob_persist_ctx, link);
1897 : 46 : next_persist = TAILQ_FIRST(&blob->persists_to_complete);
1898 : :
1899 : 46 : blob->state = SPDK_BLOB_STATE_DIRTY;
1900 : 46 : bs_mark_dirty(seq, blob->bs, blob_persist_start, next_persist);
1901 : : }
1902 : :
1903 : : static void
1904 : 10131 : blob_persist_clear_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1905 : : {
1906 : 10131 : struct spdk_blob_persist_ctx *ctx = cb_arg;
1907 : 10131 : struct spdk_blob *blob = ctx->blob;
1908 : 10131 : struct spdk_blob_store *bs = blob->bs;
1909 : : size_t i;
1910 : :
1911 [ - + ]: 10131 : if (bserrno != 0) {
1912 : 0 : blob_persist_complete(seq, ctx, bserrno);
1913 : 0 : return;
1914 : : }
1915 : :
1916 : 10131 : spdk_spin_lock(&bs->used_lock);
1917 : :
1918 : : /* Release all extent_pages that were truncated */
1919 [ + + ]: 13607 : for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
1920 : : /* Nothing to release if it was not allocated */
1921 [ + + ]: 3476 : if (blob->active.extent_pages[i] != 0) {
1922 : 1254 : bs_release_md_page(bs, blob->active.extent_pages[i]);
1923 : : }
1924 : : }
1925 : :
1926 : 10131 : spdk_spin_unlock(&bs->used_lock);
1927 : :
1928 [ + + ]: 10131 : if (blob->active.num_extent_pages == 0) {
1929 : 7302 : free(blob->active.extent_pages);
1930 : 7302 : blob->active.extent_pages = NULL;
1931 : 7302 : blob->active.extent_pages_array_size = 0;
1932 [ + + ]: 2829 : } else if (blob->active.num_extent_pages != blob->active.extent_pages_array_size) {
1933 : : #ifndef __clang_analyzer__
1934 : : void *tmp;
1935 : :
1936 : : /* scan-build really can't figure reallocs, workaround it */
1937 : 4 : tmp = realloc(blob->active.extent_pages, sizeof(uint32_t) * blob->active.num_extent_pages);
1938 [ - + ]: 4 : assert(tmp != NULL);
1939 : 4 : blob->active.extent_pages = tmp;
1940 : : #endif
1941 : 4 : blob->active.extent_pages_array_size = blob->active.num_extent_pages;
1942 : : }
1943 : :
1944 : 10131 : blob_persist_complete(seq, ctx, bserrno);
1945 : : }
1946 : :
1947 : : static void
1948 : 10131 : blob_persist_clear_extents(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
1949 : : {
1950 : 10131 : struct spdk_blob *blob = ctx->blob;
1951 : 10131 : struct spdk_blob_store *bs = blob->bs;
1952 : : size_t i;
1953 : : uint64_t lba;
1954 : : uint64_t lba_count;
1955 : : spdk_bs_batch_t *batch;
1956 : :
1957 : 10131 : batch = bs_sequence_to_batch(seq, blob_persist_clear_extents_cpl, ctx);
1958 : 10131 : lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
1959 : :
1960 : : /* Clear all extent_pages that were truncated */
1961 [ + + ]: 13607 : for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
1962 : : /* Nothing to clear if it was not allocated */
1963 [ + + ]: 3476 : if (blob->active.extent_pages[i] != 0) {
1964 : 1254 : lba = bs_md_page_to_lba(bs, blob->active.extent_pages[i]);
1965 : 1254 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
1966 : : }
1967 : : }
1968 : :
1969 : 10131 : bs_batch_close(batch);
1970 : 10131 : }
1971 : :
1972 : : static void
1973 : 10131 : blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1974 : : {
1975 : 10131 : struct spdk_blob_persist_ctx *ctx = cb_arg;
1976 : 10131 : struct spdk_blob *blob = ctx->blob;
1977 : 10131 : struct spdk_blob_store *bs = blob->bs;
1978 : : size_t i;
1979 : :
1980 [ - + ]: 10131 : if (bserrno != 0) {
1981 : 0 : blob_persist_complete(seq, ctx, bserrno);
1982 : 0 : return;
1983 : : }
1984 : :
1985 : 10131 : spdk_spin_lock(&bs->used_lock);
1986 : : /* Release all clusters that were truncated */
1987 [ + + ]: 2148401 : for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
1988 : 2138270 : uint32_t cluster_num = bs_lba_to_cluster(bs, blob->active.clusters[i]);
1989 : :
1990 : : /* Nothing to release if it was not allocated */
1991 [ + + ]: 2138270 : if (blob->active.clusters[i] != 0) {
1992 : 4750 : bs_release_cluster(bs, cluster_num);
1993 : : }
1994 : : }
1995 : 10131 : spdk_spin_unlock(&bs->used_lock);
1996 : :
1997 [ + + ]: 10131 : if (blob->active.num_clusters == 0) {
1998 : 3908 : free(blob->active.clusters);
1999 : 3908 : blob->active.clusters = NULL;
2000 : 3908 : blob->active.cluster_array_size = 0;
2001 [ + + ]: 6223 : } else if (blob->active.num_clusters != blob->active.cluster_array_size) {
2002 : : #ifndef __clang_analyzer__
2003 : : void *tmp;
2004 : :
2005 : : /* scan-build really can't figure reallocs, workaround it */
2006 : 28 : tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * blob->active.num_clusters);
2007 [ - + ]: 28 : assert(tmp != NULL);
2008 : 28 : blob->active.clusters = tmp;
2009 : :
2010 : : #endif
2011 : 28 : blob->active.cluster_array_size = blob->active.num_clusters;
2012 : : }
2013 : :
2014 : : /* Move on to clearing extent pages */
2015 : 10131 : blob_persist_clear_extents(seq, ctx);
2016 : : }
2017 : :
2018 : : static void
2019 : 10131 : blob_persist_clear_clusters(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
2020 : : {
2021 : 10131 : struct spdk_blob *blob = ctx->blob;
2022 : 10131 : struct spdk_blob_store *bs = blob->bs;
2023 : : spdk_bs_batch_t *batch;
2024 : : size_t i;
2025 : : uint64_t lba;
2026 : : uint64_t lba_count;
2027 : :
2028 : : /* Clusters don't move around in blobs. The list shrinks or grows
2029 : : * at the end, but no changes ever occur in the middle of the list.
2030 : : */
2031 : :
2032 : 10131 : batch = bs_sequence_to_batch(seq, blob_persist_clear_clusters_cpl, ctx);
2033 : :
2034 : : /* Clear all clusters that were truncated */
2035 : 10131 : lba = 0;
2036 : 10131 : lba_count = 0;
2037 [ + + ]: 2148401 : for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
2038 : 2138270 : uint64_t next_lba = blob->active.clusters[i];
2039 : 2138270 : uint64_t next_lba_count = bs_cluster_to_lba(bs, 1);
2040 : :
2041 [ + + + + ]: 2138270 : if (next_lba > 0 && (lba + lba_count) == next_lba) {
2042 : : /* This cluster is contiguous with the previous one. */
2043 : 2232 : lba_count += next_lba_count;
2044 : 2232 : continue;
2045 [ + + ]: 2136038 : } else if (next_lba == 0) {
2046 : 2133520 : continue;
2047 : : }
2048 : :
2049 : : /* This cluster is not contiguous with the previous one. */
2050 : :
2051 : : /* If a run of LBAs previously existing, clear them now */
2052 [ + + ]: 2518 : if (lba_count > 0) {
2053 : 72 : bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
2054 : : }
2055 : :
2056 : : /* Start building the next batch */
2057 : 2518 : lba = next_lba;
2058 [ + - ]: 2518 : if (next_lba > 0) {
2059 : 2518 : lba_count = next_lba_count;
2060 : : } else {
2061 : 0 : lba_count = 0;
2062 : : }
2063 : : }
2064 : :
2065 : : /* If we ended with a contiguous set of LBAs, clear them now */
2066 [ + + ]: 10131 : if (lba_count > 0) {
2067 : 2446 : bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
2068 : : }
2069 : :
2070 : 10131 : bs_batch_close(batch);
2071 : 10131 : }
2072 : :
2073 : : static void
2074 : 10139 : blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2075 : : {
2076 : 10139 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2077 : 10139 : struct spdk_blob *blob = ctx->blob;
2078 : 10139 : struct spdk_blob_store *bs = blob->bs;
2079 : : size_t i;
2080 : :
2081 [ + + ]: 10139 : if (bserrno != 0) {
2082 : 8 : blob_persist_complete(seq, ctx, bserrno);
2083 : 8 : return;
2084 : : }
2085 : :
2086 : 10131 : spdk_spin_lock(&bs->used_lock);
2087 : :
2088 : : /* This loop starts at 1 because the first page is special and handled
2089 : : * below. The pages (except the first) are never written in place,
2090 : : * so any pages in the clean list must be zeroed.
2091 : : */
2092 [ + + ]: 10267 : for (i = 1; i < blob->clean.num_pages; i++) {
2093 : 136 : bs_release_md_page(bs, blob->clean.pages[i]);
2094 : : }
2095 : :
2096 [ + + ]: 10131 : if (blob->active.num_pages == 0) {
2097 : : uint32_t page_num;
2098 : :
2099 : 2978 : page_num = bs_blobid_to_page(blob->id);
2100 : 2978 : bs_release_md_page(bs, page_num);
2101 : : }
2102 : :
2103 : 10131 : spdk_spin_unlock(&bs->used_lock);
2104 : :
2105 : : /* Move on to clearing clusters */
2106 : 10131 : blob_persist_clear_clusters(seq, ctx);
2107 : : }
2108 : :
2109 : : static void
2110 : 10219 : blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2111 : : {
2112 : 10219 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2113 : 10219 : struct spdk_blob *blob = ctx->blob;
2114 : 10219 : struct spdk_blob_store *bs = blob->bs;
2115 : : uint64_t lba;
2116 : : uint64_t lba_count;
2117 : : spdk_bs_batch_t *batch;
2118 : : size_t i;
2119 : :
2120 [ + + ]: 10219 : if (bserrno != 0) {
2121 : 80 : blob_persist_complete(seq, ctx, bserrno);
2122 : 80 : return;
2123 : : }
2124 : :
2125 : 10139 : batch = bs_sequence_to_batch(seq, blob_persist_zero_pages_cpl, ctx);
2126 : :
2127 : 10139 : lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
2128 : :
2129 : : /* This loop starts at 1 because the first page is special and handled
2130 : : * below. The pages (except the first) are never written in place,
2131 : : * so any pages in the clean list must be zeroed.
2132 : : */
2133 [ + + ]: 10275 : for (i = 1; i < blob->clean.num_pages; i++) {
2134 : 136 : lba = bs_md_page_to_lba(bs, blob->clean.pages[i]);
2135 : :
2136 : 136 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
2137 : : }
2138 : :
2139 : : /* The first page will only be zeroed if this is a delete. */
2140 [ + + ]: 10139 : if (blob->active.num_pages == 0) {
2141 : : uint32_t page_num;
2142 : :
2143 : : /* The first page in the metadata goes where the blobid indicates */
2144 : 2986 : page_num = bs_blobid_to_page(blob->id);
2145 : 2986 : lba = bs_md_page_to_lba(bs, page_num);
2146 : :
2147 : 2986 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
2148 : : }
2149 : :
2150 : 10139 : bs_batch_close(batch);
2151 : : }
2152 : :
2153 : : static void
2154 : 7233 : blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2155 : : {
2156 : 7233 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2157 : 7233 : struct spdk_blob *blob = ctx->blob;
2158 : 7233 : struct spdk_blob_store *bs = blob->bs;
2159 : : uint64_t lba;
2160 : : uint32_t lba_count;
2161 : : struct spdk_blob_md_page *page;
2162 : :
2163 [ - + ]: 7233 : if (bserrno != 0) {
2164 : 0 : blob_persist_complete(seq, ctx, bserrno);
2165 : 0 : return;
2166 : : }
2167 : :
2168 [ - + ]: 7233 : if (blob->active.num_pages == 0) {
2169 : : /* Move on to the next step */
2170 : 0 : blob_persist_zero_pages(seq, ctx, 0);
2171 : 0 : return;
2172 : : }
2173 : :
2174 : 7233 : lba_count = bs_byte_to_lba(bs, sizeof(*page));
2175 : :
2176 : 7233 : page = &ctx->pages[0];
2177 : : /* The first page in the metadata goes where the blobid indicates */
2178 : 7233 : lba = bs_md_page_to_lba(bs, bs_blobid_to_page(blob->id));
2179 : :
2180 : 7233 : bs_sequence_write_dev(seq, page, lba, lba_count,
2181 : : blob_persist_zero_pages, ctx);
2182 : : }
2183 : :
2184 : : static void
2185 : 7233 : blob_persist_write_page_chain(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
2186 : : {
2187 : 7233 : struct spdk_blob *blob = ctx->blob;
2188 : 7233 : struct spdk_blob_store *bs = blob->bs;
2189 : : uint64_t lba;
2190 : : uint32_t lba_count;
2191 : : struct spdk_blob_md_page *page;
2192 : : spdk_bs_batch_t *batch;
2193 : : size_t i;
2194 : :
2195 : : /* Clusters don't move around in blobs. The list shrinks or grows
2196 : : * at the end, but no changes ever occur in the middle of the list.
2197 : : */
2198 : :
2199 : 7233 : lba_count = bs_byte_to_lba(bs, sizeof(*page));
2200 : :
2201 : 7233 : batch = bs_sequence_to_batch(seq, blob_persist_write_page_root, ctx);
2202 : :
2203 : : /* This starts at 1. The root page is not written until
2204 : : * all of the others are finished
2205 : : */
2206 [ + + ]: 7409 : for (i = 1; i < blob->active.num_pages; i++) {
2207 : 176 : page = &ctx->pages[i];
2208 [ - + ]: 176 : assert(page->sequence_num == i);
2209 : :
2210 : 176 : lba = bs_md_page_to_lba(bs, blob->active.pages[i]);
2211 : :
2212 : 176 : bs_batch_write_dev(batch, page, lba, lba_count);
2213 : : }
2214 : :
2215 : 7233 : bs_batch_close(batch);
2216 : 7233 : }
2217 : :
2218 : : static int
2219 : 7170 : blob_resize(struct spdk_blob *blob, uint64_t sz)
2220 : : {
2221 : : uint64_t i;
2222 : : uint64_t *tmp;
2223 : 7162 : uint64_t cluster;
2224 : 7162 : uint32_t lfmd; /* lowest free md page */
2225 : : uint64_t num_clusters;
2226 : : uint32_t *ep_tmp;
2227 : 7170 : uint64_t new_num_ep = 0, current_num_ep = 0;
2228 : : struct spdk_blob_store *bs;
2229 : : int rc;
2230 : :
2231 : 7170 : bs = blob->bs;
2232 : :
2233 : 7170 : blob_verify_md_op(blob);
2234 : :
2235 [ + + ]: 7170 : if (blob->active.num_clusters == sz) {
2236 : 923 : return 0;
2237 : : }
2238 : :
2239 [ - + ]: 6247 : if (blob->active.num_clusters < blob->active.cluster_array_size) {
2240 : : /* If this blob was resized to be larger, then smaller, then
2241 : : * larger without syncing, then the cluster array already
2242 : : * contains spare assigned clusters we can use.
2243 : : */
2244 : 0 : num_clusters = spdk_min(blob->active.cluster_array_size,
2245 : : sz);
2246 : : } else {
2247 : 6247 : num_clusters = blob->active.num_clusters;
2248 : : }
2249 : :
2250 [ + + ]: 6247 : if (blob->use_extent_table) {
2251 : : /* Round up since every cluster beyond current Extent Table size,
2252 : : * requires new extent page. */
2253 : 3179 : new_num_ep = spdk_divide_round_up(sz, SPDK_EXTENTS_PER_EP);
2254 : 3179 : current_num_ep = spdk_divide_round_up(num_clusters, SPDK_EXTENTS_PER_EP);
2255 : : }
2256 : :
2257 [ - + ]: 6247 : assert(!spdk_spin_held(&bs->used_lock));
2258 : :
2259 : : /* Check first that we have enough clusters and md pages before we start claiming them.
2260 : : * bs->used_lock is held to ensure that clusters we think are free are still free when we go
2261 : : * to claim them later in this function.
2262 : : */
2263 [ + + + + ]: 6247 : if (sz > num_clusters && spdk_blob_is_thin_provisioned(blob) == false) {
2264 : 2611 : spdk_spin_lock(&bs->used_lock);
2265 [ + + ]: 2611 : if ((sz - num_clusters) > bs->num_free_clusters) {
2266 : 16 : rc = -ENOSPC;
2267 : 16 : goto out;
2268 : : }
2269 : 2595 : lfmd = 0;
2270 [ + + ]: 3890 : for (i = current_num_ep; i < new_num_ep ; i++) {
2271 : 1295 : lfmd = spdk_bit_array_find_first_clear(blob->bs->used_md_pages, lfmd);
2272 [ - + ]: 1295 : if (lfmd == UINT32_MAX) {
2273 : : /* No more free md pages. Cannot satisfy the request */
2274 : 0 : rc = -ENOSPC;
2275 : 0 : goto out;
2276 : : }
2277 : : }
2278 : : }
2279 : :
2280 [ + + ]: 6231 : if (sz > num_clusters) {
2281 : : /* Expand the cluster array if necessary.
2282 : : * We only shrink the array when persisting.
2283 : : */
2284 : 3419 : tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * sz);
2285 [ + - - + ]: 3419 : if (sz > 0 && tmp == NULL) {
2286 : 0 : rc = -ENOMEM;
2287 : 0 : goto out;
2288 : : }
2289 : 3419 : memset(tmp + blob->active.cluster_array_size, 0,
2290 : 3419 : sizeof(*blob->active.clusters) * (sz - blob->active.cluster_array_size));
2291 : 3419 : blob->active.clusters = tmp;
2292 : 3419 : blob->active.cluster_array_size = sz;
2293 : :
2294 : : /* Expand the extents table, only if enough clusters were added */
2295 [ + + + - ]: 3419 : if (new_num_ep > current_num_ep && blob->use_extent_table) {
2296 : 1695 : ep_tmp = realloc(blob->active.extent_pages, sizeof(*blob->active.extent_pages) * new_num_ep);
2297 [ + - - + ]: 1695 : if (new_num_ep > 0 && ep_tmp == NULL) {
2298 : 0 : rc = -ENOMEM;
2299 : 0 : goto out;
2300 : : }
2301 : 1695 : memset(ep_tmp + blob->active.extent_pages_array_size, 0,
2302 : 1695 : sizeof(*blob->active.extent_pages) * (new_num_ep - blob->active.extent_pages_array_size));
2303 : 1695 : blob->active.extent_pages = ep_tmp;
2304 : 1695 : blob->active.extent_pages_array_size = new_num_ep;
2305 : : }
2306 : : }
2307 : :
2308 : 6231 : blob->state = SPDK_BLOB_STATE_DIRTY;
2309 : :
2310 [ + + ]: 6231 : if (spdk_blob_is_thin_provisioned(blob) == false) {
2311 : 4865 : cluster = 0;
2312 : 4865 : lfmd = 0;
2313 [ + + ]: 19742 : for (i = num_clusters; i < sz; i++) {
2314 : 14877 : bs_allocate_cluster(blob, i, &cluster, &lfmd, true);
2315 : : /* Do not increment lfmd here. lfmd will get updated
2316 : : * to the md_page allocated (if any) when a new extent
2317 : : * page is needed. Just pass that value again,
2318 : : * bs_allocate_cluster will just start at that index
2319 : : * to find the next free md_page when needed.
2320 : : */
2321 : : }
2322 : : }
2323 : :
2324 : : /* If we are shrinking the blob, we must adjust num_allocated_clusters */
2325 [ + + ]: 2144581 : for (i = sz; i < num_clusters; i++) {
2326 [ + + ]: 2138350 : if (blob->active.clusters[i] != 0) {
2327 : 4750 : blob->active.num_allocated_clusters--;
2328 : : }
2329 : : }
2330 : :
2331 : 6231 : blob->active.num_clusters = sz;
2332 : 6231 : blob->active.num_extent_pages = new_num_ep;
2333 : :
2334 : 6231 : rc = 0;
2335 : 6247 : out:
2336 [ + + ]: 6247 : if (spdk_spin_held(&bs->used_lock)) {
2337 : 2611 : spdk_spin_unlock(&bs->used_lock);
2338 : : }
2339 : :
2340 : 6247 : return rc;
2341 : : }
2342 : :
2343 : : static void
2344 : 7233 : blob_persist_generate_new_md(struct spdk_blob_persist_ctx *ctx)
2345 : : {
2346 : 7233 : spdk_bs_sequence_t *seq = ctx->seq;
2347 : 7233 : struct spdk_blob *blob = ctx->blob;
2348 : 7233 : struct spdk_blob_store *bs = blob->bs;
2349 : : uint64_t i;
2350 : : uint32_t page_num;
2351 : : void *tmp;
2352 : : int rc;
2353 : :
2354 : : /* Generate the new metadata */
2355 : 7233 : rc = blob_serialize(blob, &ctx->pages, &blob->active.num_pages);
2356 [ - + ]: 7233 : if (rc < 0) {
2357 : 0 : blob_persist_complete(seq, ctx, rc);
2358 : 0 : return;
2359 : : }
2360 : :
2361 [ - + ]: 7233 : assert(blob->active.num_pages >= 1);
2362 : :
2363 : : /* Resize the cache of page indices */
2364 : 7233 : tmp = realloc(blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
2365 [ - + ]: 7233 : if (!tmp) {
2366 : 0 : blob_persist_complete(seq, ctx, -ENOMEM);
2367 : 0 : return;
2368 : : }
2369 : 7233 : blob->active.pages = tmp;
2370 : :
2371 : : /* Assign this metadata to pages. This requires two passes - one to verify that there are
2372 : : * enough pages and a second to actually claim them. The used_lock is held across
2373 : : * both passes to ensure things don't change in the middle.
2374 : : */
2375 : 7233 : spdk_spin_lock(&bs->used_lock);
2376 : 7233 : page_num = 0;
2377 : : /* Note that this loop starts at one. The first page location is fixed by the blobid. */
2378 [ + + ]: 7409 : for (i = 1; i < blob->active.num_pages; i++) {
2379 : 176 : page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
2380 [ - + ]: 176 : if (page_num == UINT32_MAX) {
2381 : 0 : spdk_spin_unlock(&bs->used_lock);
2382 : 0 : blob_persist_complete(seq, ctx, -ENOMEM);
2383 : 0 : return;
2384 : : }
2385 : 176 : page_num++;
2386 : : }
2387 : :
2388 : 7233 : page_num = 0;
2389 : 7233 : blob->active.pages[0] = bs_blobid_to_page(blob->id);
2390 [ + + ]: 7409 : for (i = 1; i < blob->active.num_pages; i++) {
2391 : 176 : page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
2392 : 176 : ctx->pages[i - 1].next = page_num;
2393 : : /* Now that previous metadata page is complete, calculate the crc for it. */
2394 : 176 : ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
2395 : 176 : blob->active.pages[i] = page_num;
2396 : 176 : bs_claim_md_page(bs, page_num);
2397 [ - + ]: 176 : SPDK_DEBUGLOG(blob, "Claiming page %u for blob 0x%" PRIx64 "\n", page_num,
2398 : : blob->id);
2399 : 176 : page_num++;
2400 : : }
2401 : 7233 : spdk_spin_unlock(&bs->used_lock);
2402 : 7233 : ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
2403 : : /* Start writing the metadata from last page to first */
2404 : 7233 : blob->state = SPDK_BLOB_STATE_CLEAN;
2405 : 7233 : blob_persist_write_page_chain(seq, ctx);
2406 : : }
2407 : :
2408 : : static void
2409 : 4730 : blob_persist_write_extent_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2410 : : {
2411 : 4730 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2412 : 4730 : struct spdk_blob *blob = ctx->blob;
2413 : : size_t i;
2414 : : uint32_t extent_page_id;
2415 : 4730 : uint32_t page_count = 0;
2416 : : int rc;
2417 : :
2418 [ + + ]: 4730 : if (ctx->extent_page != NULL) {
2419 : 1339 : spdk_free(ctx->extent_page);
2420 : 1339 : ctx->extent_page = NULL;
2421 : : }
2422 : :
2423 [ - + ]: 4730 : if (bserrno != 0) {
2424 : 0 : blob_persist_complete(seq, ctx, bserrno);
2425 : 4 : return;
2426 : : }
2427 : :
2428 : : /* Only write out Extent Pages when blob was resized. */
2429 [ + + ]: 9246 : for (i = ctx->next_extent_page; i < blob->active.extent_pages_array_size; i++) {
2430 : 5855 : extent_page_id = blob->active.extent_pages[i];
2431 [ + + ]: 5855 : if (extent_page_id == 0) {
2432 : : /* No Extent Page to persist */
2433 [ - + ]: 4516 : assert(spdk_blob_is_thin_provisioned(blob));
2434 : 4516 : continue;
2435 : : }
2436 [ - + ]: 1339 : assert(spdk_bit_array_get(blob->bs->used_md_pages, extent_page_id));
2437 : 1339 : ctx->next_extent_page = i + 1;
2438 : 1339 : rc = blob_serialize_add_page(ctx->blob, &ctx->extent_page, &page_count, &ctx->extent_page);
2439 [ - + ]: 1339 : if (rc < 0) {
2440 : 0 : blob_persist_complete(seq, ctx, rc);
2441 : 0 : return;
2442 : : }
2443 : :
2444 : 1339 : blob->state = SPDK_BLOB_STATE_DIRTY;
2445 : 1339 : blob_serialize_extent_page(blob, i * SPDK_EXTENTS_PER_EP, ctx->extent_page);
2446 : :
2447 : 1339 : ctx->extent_page->crc = blob_md_page_calc_crc(ctx->extent_page);
2448 : :
2449 : 1339 : bs_sequence_write_dev(seq, ctx->extent_page, bs_md_page_to_lba(blob->bs, extent_page_id),
2450 : 1339 : bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
2451 : : blob_persist_write_extent_pages, ctx);
2452 : 1339 : return;
2453 : : }
2454 : :
2455 : 3391 : blob_persist_generate_new_md(ctx);
2456 : : }
2457 : :
2458 : : static void
2459 : 10235 : blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2460 : : {
2461 : 10235 : struct spdk_blob_persist_ctx *ctx = cb_arg;
2462 : 10235 : struct spdk_blob *blob = ctx->blob;
2463 : :
2464 [ + + ]: 10235 : if (bserrno != 0) {
2465 : 16 : blob_persist_complete(seq, ctx, bserrno);
2466 : 16 : return;
2467 : : }
2468 : :
2469 [ + + ]: 10219 : if (blob->active.num_pages == 0) {
2470 : : /* This is the signal that the blob should be deleted.
2471 : : * Immediately jump to the clean up routine. */
2472 [ - + ]: 2986 : assert(blob->clean.num_pages > 0);
2473 : 2986 : blob->state = SPDK_BLOB_STATE_CLEAN;
2474 : 2986 : blob_persist_zero_pages(seq, ctx, 0);
2475 : 2986 : return;
2476 : :
2477 : : }
2478 : :
2479 [ + + ]: 7233 : if (blob->clean.num_clusters < blob->active.num_clusters) {
2480 : : /* Blob was resized up */
2481 [ - + ]: 3363 : assert(blob->clean.num_extent_pages <= blob->active.num_extent_pages);
2482 [ + + ]: 3363 : ctx->next_extent_page = spdk_max(1, blob->clean.num_extent_pages) - 1;
2483 [ + + ]: 3870 : } else if (blob->active.num_clusters < blob->active.cluster_array_size) {
2484 : : /* Blob was resized down */
2485 [ - + ]: 28 : assert(blob->clean.num_extent_pages >= blob->active.num_extent_pages);
2486 [ + + ]: 28 : ctx->next_extent_page = spdk_max(1, blob->active.num_extent_pages) - 1;
2487 : : } else {
2488 : : /* No change in size occurred */
2489 : 3842 : blob_persist_generate_new_md(ctx);
2490 : 3842 : return;
2491 : : }
2492 : :
2493 : 3391 : blob_persist_write_extent_pages(seq, ctx, 0);
2494 : : }
2495 : :
2496 : : struct spdk_bs_mark_dirty {
2497 : : struct spdk_blob_store *bs;
2498 : : struct spdk_bs_super_block *super;
2499 : : spdk_bs_sequence_cpl cb_fn;
2500 : : void *cb_arg;
2501 : : };
2502 : :
2503 : : static void
2504 : 320 : bs_mark_dirty_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2505 : : {
2506 : 320 : struct spdk_bs_mark_dirty *ctx = cb_arg;
2507 : :
2508 [ + + ]: 320 : if (bserrno == 0) {
2509 : 304 : ctx->bs->clean = 0;
2510 : : }
2511 : :
2512 : 320 : ctx->cb_fn(seq, ctx->cb_arg, bserrno);
2513 : :
2514 : 320 : spdk_free(ctx->super);
2515 : 320 : free(ctx);
2516 : 320 : }
2517 : :
2518 : : static void bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
2519 : : struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg);
2520 : :
2521 : :
2522 : : static void
2523 : 320 : bs_mark_dirty_write(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2524 : : {
2525 : 320 : struct spdk_bs_mark_dirty *ctx = cb_arg;
2526 : : int rc;
2527 : :
2528 [ + + ]: 320 : if (bserrno != 0) {
2529 : 8 : bs_mark_dirty_write_cpl(seq, ctx, bserrno);
2530 : 8 : return;
2531 : : }
2532 : :
2533 : 312 : rc = bs_super_validate(ctx->super, ctx->bs);
2534 [ - + ]: 312 : if (rc != 0) {
2535 : 0 : bs_mark_dirty_write_cpl(seq, ctx, rc);
2536 : 0 : return;
2537 : : }
2538 : :
2539 : 312 : ctx->super->clean = 0;
2540 [ + + ]: 312 : if (ctx->super->size == 0) {
2541 : 8 : ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
2542 : : }
2543 : :
2544 : 312 : bs_write_super(seq, ctx->bs, ctx->super, bs_mark_dirty_write_cpl, ctx);
2545 : : }
2546 : :
2547 : : static void
2548 : 11109 : bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
2549 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg)
2550 : : {
2551 : : struct spdk_bs_mark_dirty *ctx;
2552 : :
2553 : : /* Blobstore is already marked dirty */
2554 [ + + ]: 11109 : if (bs->clean == 0) {
2555 : 10789 : cb_fn(seq, cb_arg, 0);
2556 : 10789 : return;
2557 : : }
2558 : :
2559 : 320 : ctx = calloc(1, sizeof(*ctx));
2560 [ - + ]: 320 : if (!ctx) {
2561 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
2562 : 0 : return;
2563 : : }
2564 : 320 : ctx->bs = bs;
2565 : 320 : ctx->cb_fn = cb_fn;
2566 : 320 : ctx->cb_arg = cb_arg;
2567 : :
2568 : 320 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
2569 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
2570 [ - + ]: 320 : if (!ctx->super) {
2571 : 0 : free(ctx);
2572 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
2573 : 0 : return;
2574 : : }
2575 : :
2576 : 320 : bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
2577 : 320 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
2578 : : bs_mark_dirty_write, ctx);
2579 : : }
2580 : :
2581 : : /* Write a blob to disk */
2582 : : static void
2583 : 18369 : blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
2584 : : spdk_bs_sequence_cpl cb_fn, void *cb_arg)
2585 : : {
2586 : : struct spdk_blob_persist_ctx *ctx;
2587 : :
2588 : 18369 : blob_verify_md_op(blob);
2589 : :
2590 [ + + + + ]: 18369 : if (blob->state == SPDK_BLOB_STATE_CLEAN && TAILQ_EMPTY(&blob->persists_to_complete)) {
2591 : 8134 : cb_fn(seq, cb_arg, 0);
2592 : 8134 : return;
2593 : : }
2594 : :
2595 : 10235 : ctx = calloc(1, sizeof(*ctx));
2596 [ - + ]: 10235 : if (!ctx) {
2597 : 0 : cb_fn(seq, cb_arg, -ENOMEM);
2598 : 0 : return;
2599 : : }
2600 : 10235 : ctx->blob = blob;
2601 : 10235 : ctx->seq = seq;
2602 : 10235 : ctx->cb_fn = cb_fn;
2603 : 10235 : ctx->cb_arg = cb_arg;
2604 : :
2605 : : /* Multiple blob persists can affect one another, via blob->state or
2606 : : * blob mutable data changes. To prevent it, queue up the persists. */
2607 [ + + ]: 10235 : if (!TAILQ_EMPTY(&blob->persists_to_complete)) {
2608 : 46 : TAILQ_INSERT_TAIL(&blob->pending_persists, ctx, link);
2609 : 46 : return;
2610 : : }
2611 [ - + ]: 10189 : TAILQ_INSERT_HEAD(&blob->persists_to_complete, ctx, link);
2612 : :
2613 : 10189 : bs_mark_dirty(seq, blob->bs, blob_persist_start, ctx);
2614 : : }
2615 : :
2616 : : struct spdk_blob_copy_cluster_ctx {
2617 : : struct spdk_blob *blob;
2618 : : uint8_t *buf;
2619 : : uint64_t page;
2620 : : uint64_t new_cluster;
2621 : : uint32_t new_extent_page;
2622 : : spdk_bs_sequence_t *seq;
2623 : : struct spdk_blob_md_page *new_cluster_page;
2624 : : };
2625 : :
2626 : : struct spdk_blob_free_cluster_ctx {
2627 : : struct spdk_blob *blob;
2628 : : uint64_t page;
2629 : : struct spdk_blob_md_page *md_page;
2630 : : uint64_t cluster_num;
2631 : : uint32_t extent_page;
2632 : : spdk_bs_sequence_t *seq;
2633 : : };
2634 : :
2635 : : static void
2636 : 1630 : blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno)
2637 : : {
2638 : 1630 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2639 : 1630 : struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq;
2640 : 1630 : TAILQ_HEAD(, spdk_bs_request_set) requests;
2641 : : spdk_bs_user_op_t *op;
2642 : :
2643 : 1630 : TAILQ_INIT(&requests);
2644 [ - + + - ]: 1630 : TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link);
2645 : :
2646 [ + + ]: 3510 : while (!TAILQ_EMPTY(&requests)) {
2647 : 1880 : op = TAILQ_FIRST(&requests);
2648 [ + + ]: 1880 : TAILQ_REMOVE(&requests, op, link);
2649 [ + - ]: 1880 : if (bserrno == 0) {
2650 : 1880 : bs_user_op_execute(op);
2651 : : } else {
2652 : 0 : bs_user_op_abort(op, bserrno);
2653 : : }
2654 : : }
2655 : :
2656 : 1630 : spdk_free(ctx->buf);
2657 : 1630 : free(ctx);
2658 : 1630 : }
2659 : :
2660 : : static void
2661 : 120 : blob_free_cluster_cpl(void *cb_arg, int bserrno)
2662 : : {
2663 : 120 : struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
2664 : 120 : spdk_bs_sequence_t *seq = ctx->seq;
2665 : :
2666 : 120 : bs_sequence_finish(seq, bserrno);
2667 : :
2668 : 120 : free(ctx);
2669 : 120 : }
2670 : :
2671 : : static void
2672 : 8 : blob_insert_cluster_revert(struct spdk_blob_copy_cluster_ctx *ctx)
2673 : : {
2674 : 8 : spdk_spin_lock(&ctx->blob->bs->used_lock);
2675 : 8 : bs_release_cluster(ctx->blob->bs, ctx->new_cluster);
2676 [ + + ]: 8 : if (ctx->new_extent_page != 0) {
2677 : 4 : bs_release_md_page(ctx->blob->bs, ctx->new_extent_page);
2678 : : }
2679 : 8 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
2680 : 8 : }
2681 : :
2682 : : static void
2683 : 8 : blob_insert_cluster_clear_cpl(void *cb_arg, int bserrno)
2684 : : {
2685 : 8 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2686 : :
2687 [ - + ]: 8 : if (bserrno) {
2688 : 0 : SPDK_WARNLOG("Failed to clear cluster: %d\n", bserrno);
2689 : : }
2690 : :
2691 : 8 : blob_insert_cluster_revert(ctx);
2692 : 8 : bs_sequence_finish(ctx->seq, bserrno);
2693 : 8 : }
2694 : :
2695 : : static void
2696 : 8 : blob_insert_cluster_clear(struct spdk_blob_copy_cluster_ctx *ctx)
2697 : : {
2698 : 8 : struct spdk_bs_cpl cpl;
2699 : : spdk_bs_batch_t *batch;
2700 : 8 : struct spdk_io_channel *ch = spdk_io_channel_from_ctx(ctx->seq->channel);
2701 : :
2702 : : /*
2703 : : * We allocated a cluster and we copied data to it. But now, we realized that we don't need
2704 : : * this cluster and we want to release it. We must ensure that we clear the data on this
2705 : : * cluster.
2706 : : * The cluster may later be re-allocated by a thick-provisioned blob for example. When
2707 : : * reading from this thick-provisioned blob before writing data, we should read zeroes.
2708 : : */
2709 : :
2710 : 8 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2711 : 8 : cpl.u.blob_basic.cb_fn = blob_insert_cluster_clear_cpl;
2712 : 8 : cpl.u.blob_basic.cb_arg = ctx;
2713 : :
2714 : 8 : batch = bs_batch_open(ch, &cpl, ctx->blob);
2715 [ - + ]: 8 : if (!batch) {
2716 : 0 : blob_insert_cluster_clear_cpl(ctx, -ENOMEM);
2717 : 0 : return;
2718 : : }
2719 : :
2720 : 8 : bs_batch_clear_dev(ctx->blob, batch, bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
2721 : 8 : bs_cluster_to_lba(ctx->blob->bs, 1));
2722 : 8 : bs_batch_close(batch);
2723 : : }
2724 : :
2725 : : static void
2726 : 1630 : blob_insert_cluster_cpl(void *cb_arg, int bserrno)
2727 : : {
2728 : 1630 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2729 : :
2730 [ + + ]: 1630 : if (bserrno) {
2731 [ + - ]: 8 : if (bserrno == -EEXIST) {
2732 : : /* The metadata insert failed because another thread
2733 : : * allocated the cluster first. Clear and free our cluster
2734 : : * but continue without error. */
2735 : 8 : blob_insert_cluster_clear(ctx);
2736 : 8 : return;
2737 : : }
2738 : :
2739 : 0 : blob_insert_cluster_revert(ctx);
2740 : : }
2741 : :
2742 : 1622 : bs_sequence_finish(ctx->seq, bserrno);
2743 : : }
2744 : :
2745 : : static void
2746 : 816 : blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2747 : : {
2748 : 816 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2749 : : uint32_t cluster_number;
2750 : :
2751 [ - + ]: 816 : if (bserrno) {
2752 : : /* The write failed, so jump to the final completion handler */
2753 : 0 : bs_sequence_finish(seq, bserrno);
2754 : 0 : return;
2755 : : }
2756 : :
2757 : 816 : cluster_number = bs_page_to_cluster(ctx->blob->bs, ctx->page);
2758 : :
2759 : 816 : blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
2760 : : ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
2761 : : }
2762 : :
2763 : : static void
2764 : 560 : blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2765 : : {
2766 : 560 : struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2767 : :
2768 [ - + ]: 560 : if (bserrno != 0) {
2769 : : /* The read failed, so jump to the final completion handler */
2770 : 0 : bs_sequence_finish(seq, bserrno);
2771 : 0 : return;
2772 : : }
2773 : :
2774 : : /* Write whole cluster */
2775 : 560 : bs_sequence_write_dev(seq, ctx->buf,
2776 : 560 : bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
2777 : 560 : bs_cluster_to_lba(ctx->blob->bs, 1),
2778 : : blob_write_copy_cpl, ctx);
2779 : : }
2780 : :
2781 : : static bool
2782 : 1598 : blob_can_copy(struct spdk_blob *blob, uint32_t cluster_start_page, uint64_t *base_lba)
2783 : : {
2784 : 1598 : uint64_t lba = bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page);
2785 : :
2786 [ + + + + : 2298 : return (!blob_is_esnap_clone(blob) && blob->bs->dev->copy != NULL) &&
+ + ]
2787 : 700 : blob->back_bs_dev->translate_lba(blob->back_bs_dev, lba, base_lba);
2788 : : }
2789 : :
2790 : : static void
2791 : 256 : blob_copy(struct spdk_blob_copy_cluster_ctx *ctx, spdk_bs_user_op_t *op, uint64_t src_lba)
2792 : : {
2793 : 256 : struct spdk_blob *blob = ctx->blob;
2794 : 256 : uint64_t lba_count = bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz);
2795 : :
2796 : 256 : bs_sequence_copy_dev(ctx->seq,
2797 : 256 : bs_cluster_to_lba(blob->bs, ctx->new_cluster),
2798 : : src_lba,
2799 : : lba_count,
2800 : : blob_write_copy_cpl, ctx);
2801 : 256 : }
2802 : :
2803 : : static void
2804 : 1880 : bs_allocate_and_copy_cluster(struct spdk_blob *blob,
2805 : : struct spdk_io_channel *_ch,
2806 : : uint64_t io_unit, spdk_bs_user_op_t *op)
2807 : : {
2808 : 1880 : struct spdk_bs_cpl cpl;
2809 : : struct spdk_bs_channel *ch;
2810 : : struct spdk_blob_copy_cluster_ctx *ctx;
2811 : : uint32_t cluster_start_page;
2812 : : uint32_t cluster_number;
2813 : : bool is_zeroes;
2814 : : bool can_copy;
2815 : : bool is_valid_range;
2816 : 1880 : uint64_t copy_src_lba;
2817 : : int rc;
2818 : :
2819 : 1880 : ch = spdk_io_channel_get_ctx(_ch);
2820 : :
2821 [ + + ]: 1880 : if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) {
2822 : : /* There are already operations pending. Queue this user op
2823 : : * and return because it will be re-executed when the outstanding
2824 : : * cluster allocation completes. */
2825 : 250 : TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
2826 : 250 : return;
2827 : : }
2828 : :
2829 : : /* Round the io_unit offset down to the first page in the cluster */
2830 : 1630 : cluster_start_page = bs_io_unit_to_cluster_start(blob, io_unit);
2831 : :
2832 : : /* Calculate which index in the metadata cluster array the corresponding
2833 : : * cluster is supposed to be at. */
2834 : 1630 : cluster_number = bs_io_unit_to_cluster_number(blob, io_unit);
2835 : :
2836 : 1630 : ctx = calloc(1, sizeof(*ctx));
2837 [ - + ]: 1630 : if (!ctx) {
2838 : 0 : bs_user_op_abort(op, -ENOMEM);
2839 : 0 : return;
2840 : : }
2841 : :
2842 [ - + ]: 1630 : assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0);
2843 : :
2844 : 1630 : ctx->blob = blob;
2845 : 1630 : ctx->page = cluster_start_page;
2846 : 1630 : ctx->new_cluster_page = ch->new_cluster_page;
2847 : 1630 : memset(ctx->new_cluster_page, 0, SPDK_BS_PAGE_SIZE);
2848 : :
2849 : : /* Check if the cluster that we intend to do CoW for is valid for
2850 : : * the backing dev. For zeroes backing dev, it'll be always valid.
2851 : : * For other backing dev e.g. a snapshot, it could be invalid if
2852 : : * the blob has been resized after snapshot was taken. */
2853 : 1630 : is_valid_range = blob->back_bs_dev->is_range_valid(blob->back_bs_dev,
2854 : : bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
2855 : 1630 : bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
2856 : :
2857 [ + + + + ]: 1630 : can_copy = is_valid_range && blob_can_copy(blob, cluster_start_page, ©_src_lba);
2858 : :
2859 [ + + + + ]: 3228 : is_zeroes = is_valid_range && blob->back_bs_dev->is_zeroes(blob->back_bs_dev,
2860 : : bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
2861 : 1598 : bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
2862 [ + + + + : 1630 : if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes && !can_copy) {
+ + ]
2863 : 560 : ctx->buf = spdk_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen,
2864 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
2865 [ - + ]: 560 : if (!ctx->buf) {
2866 : 0 : SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n",
2867 : : blob->bs->cluster_sz);
2868 : 0 : free(ctx);
2869 : 0 : bs_user_op_abort(op, -ENOMEM);
2870 : 0 : return;
2871 : : }
2872 : : }
2873 : :
2874 : 1630 : spdk_spin_lock(&blob->bs->used_lock);
2875 : 1630 : rc = bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, &ctx->new_extent_page,
2876 : : false);
2877 : 1630 : spdk_spin_unlock(&blob->bs->used_lock);
2878 [ - + ]: 1630 : if (rc != 0) {
2879 : 0 : spdk_free(ctx->buf);
2880 : 0 : free(ctx);
2881 : 0 : bs_user_op_abort(op, rc);
2882 : 0 : return;
2883 : : }
2884 : :
2885 : 1630 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2886 : 1630 : cpl.u.blob_basic.cb_fn = blob_allocate_and_copy_cluster_cpl;
2887 : 1630 : cpl.u.blob_basic.cb_arg = ctx;
2888 : :
2889 : 1630 : ctx->seq = bs_sequence_start_blob(_ch, &cpl, blob);
2890 [ - + ]: 1630 : if (!ctx->seq) {
2891 : 0 : spdk_spin_lock(&blob->bs->used_lock);
2892 : 0 : bs_release_cluster(blob->bs, ctx->new_cluster);
2893 : 0 : spdk_spin_unlock(&blob->bs->used_lock);
2894 : 0 : spdk_free(ctx->buf);
2895 : 0 : free(ctx);
2896 : 0 : bs_user_op_abort(op, -ENOMEM);
2897 : 0 : return;
2898 : : }
2899 : :
2900 : : /* Queue the user op to block other incoming operations */
2901 : 1630 : TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
2902 : :
2903 [ + + + + ]: 1630 : if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes) {
2904 [ + + ]: 816 : if (can_copy) {
2905 : 256 : blob_copy(ctx, op, copy_src_lba);
2906 : : } else {
2907 : : /* Read cluster from backing device */
2908 : 560 : bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf,
2909 : : bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
2910 : 560 : bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz),
2911 : : blob_write_copy, ctx);
2912 : : }
2913 : :
2914 : : } else {
2915 : 814 : blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
2916 : : ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
2917 : : }
2918 : : }
2919 : :
2920 : : static inline bool
2921 : 81610 : blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length,
2922 : : uint64_t *lba, uint64_t *lba_count)
2923 : : {
2924 : 81610 : *lba_count = length;
2925 : :
2926 [ + + ]: 81610 : if (!bs_io_unit_is_allocated(blob, io_unit)) {
2927 [ - + ]: 6276 : assert(blob->back_bs_dev != NULL);
2928 : 6276 : *lba = bs_io_unit_to_back_dev_lba(blob, io_unit);
2929 : 6276 : *lba_count = bs_io_unit_to_back_dev_lba(blob, *lba_count);
2930 : 6276 : return false;
2931 : : } else {
2932 : 75334 : *lba = bs_blob_io_unit_to_lba(blob, io_unit);
2933 : 75334 : return true;
2934 : : }
2935 : : }
2936 : :
2937 : : struct op_split_ctx {
2938 : : struct spdk_blob *blob;
2939 : : struct spdk_io_channel *channel;
2940 : : uint64_t io_unit_offset;
2941 : : uint64_t io_units_remaining;
2942 : : void *curr_payload;
2943 : : enum spdk_blob_op_type op_type;
2944 : : spdk_bs_sequence_t *seq;
2945 : : bool in_submit_ctx;
2946 : : bool completed_in_submit_ctx;
2947 : : bool done;
2948 : : };
2949 : :
2950 : : static void
2951 : 1548 : blob_request_submit_op_split_next(void *cb_arg, int bserrno)
2952 : : {
2953 : 1548 : struct op_split_ctx *ctx = cb_arg;
2954 : 1548 : struct spdk_blob *blob = ctx->blob;
2955 : 1548 : struct spdk_io_channel *ch = ctx->channel;
2956 : 1548 : enum spdk_blob_op_type op_type = ctx->op_type;
2957 : : uint8_t *buf;
2958 : : uint64_t offset;
2959 : : uint64_t length;
2960 : : uint64_t op_length;
2961 : :
2962 [ + - + + ]: 1548 : if (bserrno != 0 || ctx->io_units_remaining == 0) {
2963 : 356 : bs_sequence_finish(ctx->seq, bserrno);
2964 [ + + ]: 356 : if (ctx->in_submit_ctx) {
2965 : : /* Defer freeing of the ctx object, since it will be
2966 : : * accessed when this unwinds back to the submisison
2967 : : * context.
2968 : : */
2969 : 80 : ctx->done = true;
2970 : : } else {
2971 : 276 : free(ctx);
2972 : : }
2973 : 356 : return;
2974 : : }
2975 : :
2976 [ + + ]: 1192 : if (ctx->in_submit_ctx) {
2977 : : /* If this split operation completed in the context
2978 : : * of its submission, mark the flag and return immediately
2979 : : * to avoid recursion.
2980 : : */
2981 : 136 : ctx->completed_in_submit_ctx = true;
2982 : 136 : return;
2983 : : }
2984 : :
2985 : : while (true) {
2986 : 1192 : ctx->completed_in_submit_ctx = false;
2987 : :
2988 : 1192 : offset = ctx->io_unit_offset;
2989 : 1192 : length = ctx->io_units_remaining;
2990 : 1192 : buf = ctx->curr_payload;
2991 [ + + ]: 1192 : op_length = spdk_min(length, bs_num_io_units_to_cluster_boundary(blob,
2992 : : offset));
2993 : :
2994 : : /* Update length and payload for next operation */
2995 : 1192 : ctx->io_units_remaining -= op_length;
2996 : 1192 : ctx->io_unit_offset += op_length;
2997 [ + + + + ]: 1192 : if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) {
2998 : 1056 : ctx->curr_payload += op_length * blob->bs->io_unit_size;
2999 : : }
3000 : :
3001 [ - + ]: 1192 : assert(!ctx->in_submit_ctx);
3002 : 1192 : ctx->in_submit_ctx = true;
3003 : :
3004 [ + + + + : 1192 : switch (op_type) {
- - ]
3005 : 836 : case SPDK_BLOB_READ:
3006 : 836 : spdk_blob_io_read(blob, ch, buf, offset, op_length,
3007 : : blob_request_submit_op_split_next, ctx);
3008 : 836 : break;
3009 : 220 : case SPDK_BLOB_WRITE:
3010 : 220 : spdk_blob_io_write(blob, ch, buf, offset, op_length,
3011 : : blob_request_submit_op_split_next, ctx);
3012 : 220 : break;
3013 : 72 : case SPDK_BLOB_UNMAP:
3014 : 72 : spdk_blob_io_unmap(blob, ch, offset, op_length,
3015 : : blob_request_submit_op_split_next, ctx);
3016 : 72 : break;
3017 : 64 : case SPDK_BLOB_WRITE_ZEROES:
3018 : 64 : spdk_blob_io_write_zeroes(blob, ch, offset, op_length,
3019 : : blob_request_submit_op_split_next, ctx);
3020 : 64 : break;
3021 : 0 : case SPDK_BLOB_READV:
3022 : : case SPDK_BLOB_WRITEV:
3023 : 0 : SPDK_ERRLOG("readv/write not valid\n");
3024 : 0 : bs_sequence_finish(ctx->seq, -EINVAL);
3025 : 0 : free(ctx);
3026 : 0 : return;
3027 : : }
3028 : :
3029 : : #ifndef __clang_analyzer__
3030 : : /* scan-build reports a false positive around accessing the ctx here. It
3031 : : * forms a path that recursively calls this function, but then says
3032 : : * "assuming ctx->in_submit_ctx is false", when that isn't possible.
3033 : : * This path does free(ctx), returns to here, and reports a use-after-free
3034 : : * bug. Wrapping this bit of code so that scan-build doesn't see it
3035 : : * works around the scan-build bug.
3036 : : */
3037 [ - + ]: 1192 : assert(ctx->in_submit_ctx);
3038 : 1192 : ctx->in_submit_ctx = false;
3039 : :
3040 : : /* If the operation completed immediately, loop back and submit the
3041 : : * next operation. Otherwise we can return and the next split
3042 : : * operation will get submitted when this current operation is
3043 : : * later completed asynchronously.
3044 : : */
3045 [ + + ]: 1192 : if (ctx->completed_in_submit_ctx) {
3046 : 136 : continue;
3047 [ + + ]: 1056 : } else if (ctx->done) {
3048 : 80 : free(ctx);
3049 : : }
3050 : : #endif
3051 : 1056 : break;
3052 : : }
3053 : : }
3054 : :
3055 : : static void
3056 : 356 : blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob,
3057 : : void *payload, uint64_t offset, uint64_t length,
3058 : : spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
3059 : : {
3060 : : struct op_split_ctx *ctx;
3061 : : spdk_bs_sequence_t *seq;
3062 : 356 : struct spdk_bs_cpl cpl;
3063 : :
3064 [ - + ]: 356 : assert(blob != NULL);
3065 : :
3066 : 356 : ctx = calloc(1, sizeof(struct op_split_ctx));
3067 [ - + ]: 356 : if (ctx == NULL) {
3068 : 0 : cb_fn(cb_arg, -ENOMEM);
3069 : 0 : return;
3070 : : }
3071 : :
3072 : 356 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3073 : 356 : cpl.u.blob_basic.cb_fn = cb_fn;
3074 : 356 : cpl.u.blob_basic.cb_arg = cb_arg;
3075 : :
3076 : 356 : seq = bs_sequence_start_blob(ch, &cpl, blob);
3077 [ - + ]: 356 : if (!seq) {
3078 : 0 : free(ctx);
3079 : 0 : cb_fn(cb_arg, -ENOMEM);
3080 : 0 : return;
3081 : : }
3082 : :
3083 : 356 : ctx->blob = blob;
3084 : 356 : ctx->channel = ch;
3085 : 356 : ctx->curr_payload = payload;
3086 : 356 : ctx->io_unit_offset = offset;
3087 : 356 : ctx->io_units_remaining = length;
3088 : 356 : ctx->op_type = op_type;
3089 : 356 : ctx->seq = seq;
3090 : :
3091 : 356 : blob_request_submit_op_split_next(ctx, 0);
3092 : : }
3093 : :
3094 : : static void
3095 : 120 : spdk_free_cluster_unmap_complete(void *cb_arg, int bserrno)
3096 : : {
3097 : 120 : struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
3098 : :
3099 [ - + ]: 120 : if (bserrno) {
3100 : 0 : bs_sequence_finish(ctx->seq, bserrno);
3101 : 0 : free(ctx);
3102 : 0 : return;
3103 : : }
3104 : :
3105 : 120 : blob_free_cluster_on_md_thread(ctx->blob, ctx->cluster_num,
3106 : : ctx->extent_page, ctx->md_page, blob_free_cluster_cpl, ctx);
3107 : : }
3108 : :
3109 : : static void
3110 : 75678 : blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob,
3111 : : void *payload, uint64_t offset, uint64_t length,
3112 : : spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
3113 : : {
3114 : 75670 : struct spdk_bs_cpl cpl;
3115 : 75670 : uint64_t lba;
3116 : 75670 : uint64_t lba_count;
3117 : : bool is_allocated;
3118 : :
3119 [ - + ]: 75678 : assert(blob != NULL);
3120 : :
3121 : 75678 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3122 : 75678 : cpl.u.blob_basic.cb_fn = cb_fn;
3123 : 75678 : cpl.u.blob_basic.cb_arg = cb_arg;
3124 : :
3125 [ + + ]: 75678 : if (blob->frozen_refcnt) {
3126 : : /* This blob I/O is frozen */
3127 : : spdk_bs_user_op_t *op;
3128 : 8 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
3129 : :
3130 : 8 : op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
3131 [ - + ]: 8 : if (!op) {
3132 : 0 : cb_fn(cb_arg, -ENOMEM);
3133 : 0 : return;
3134 : : }
3135 : :
3136 : 8 : TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
3137 : :
3138 : 8 : return;
3139 : : }
3140 : :
3141 : 75670 : is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
3142 : :
3143 [ + + + - : 75670 : switch (op_type) {
- ]
3144 : 33774 : case SPDK_BLOB_READ: {
3145 : : spdk_bs_batch_t *batch;
3146 : :
3147 : 33774 : batch = bs_batch_open(_ch, &cpl, blob);
3148 [ - + ]: 33774 : if (!batch) {
3149 : 0 : cb_fn(cb_arg, -ENOMEM);
3150 : 0 : return;
3151 : : }
3152 : :
3153 [ + + ]: 33774 : if (is_allocated) {
3154 : : /* Read from the blob */
3155 : 31598 : bs_batch_read_dev(batch, payload, lba, lba_count);
3156 : : } else {
3157 : : /* Read from the backing block device */
3158 : 2176 : bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count);
3159 : : }
3160 : :
3161 : 33774 : bs_batch_close(batch);
3162 : 33774 : break;
3163 : : }
3164 : 41702 : case SPDK_BLOB_WRITE:
3165 : : case SPDK_BLOB_WRITE_ZEROES: {
3166 [ + + ]: 41702 : if (is_allocated) {
3167 : : /* Write to the blob */
3168 : : spdk_bs_batch_t *batch;
3169 : :
3170 [ - + ]: 41014 : if (lba_count == 0) {
3171 : 0 : cb_fn(cb_arg, 0);
3172 : 0 : return;
3173 : : }
3174 : :
3175 : 41014 : batch = bs_batch_open(_ch, &cpl, blob);
3176 [ - + ]: 41014 : if (!batch) {
3177 : 0 : cb_fn(cb_arg, -ENOMEM);
3178 : 0 : return;
3179 : : }
3180 : :
3181 [ + + ]: 41014 : if (op_type == SPDK_BLOB_WRITE) {
3182 : 40950 : bs_batch_write_dev(batch, payload, lba, lba_count);
3183 : : } else {
3184 : 64 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
3185 : : }
3186 : :
3187 : 41014 : bs_batch_close(batch);
3188 : : } else {
3189 : : /* Queue this operation and allocate the cluster */
3190 : : spdk_bs_user_op_t *op;
3191 : :
3192 : 688 : op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
3193 [ - + ]: 688 : if (!op) {
3194 : 0 : cb_fn(cb_arg, -ENOMEM);
3195 : 0 : return;
3196 : : }
3197 : :
3198 : 688 : bs_allocate_and_copy_cluster(blob, _ch, offset, op);
3199 : : }
3200 : 41702 : break;
3201 : : }
3202 : 194 : case SPDK_BLOB_UNMAP: {
3203 : 194 : struct spdk_blob_free_cluster_ctx *ctx = NULL;
3204 : : spdk_bs_batch_t *batch;
3205 : :
3206 : : /* if aligned with cluster release cluster */
3207 [ + + + - : 330 : if (spdk_blob_is_thin_provisioned(blob) && is_allocated &&
+ + ]
3208 : 136 : bs_io_units_per_cluster(blob) == length) {
3209 : 120 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
3210 : : uint32_t cluster_start_page;
3211 : : uint32_t cluster_number;
3212 : :
3213 [ - + ]: 120 : assert(offset % bs_io_units_per_cluster(blob) == 0);
3214 : :
3215 : : /* Round the io_unit offset down to the first page in the cluster */
3216 : 120 : cluster_start_page = bs_io_unit_to_cluster_start(blob, offset);
3217 : :
3218 : : /* Calculate which index in the metadata cluster array the corresponding
3219 : : * cluster is supposed to be at. */
3220 : 120 : cluster_number = bs_io_unit_to_cluster_number(blob, offset);
3221 : :
3222 : 120 : ctx = calloc(1, sizeof(*ctx));
3223 [ - + ]: 120 : if (!ctx) {
3224 : 0 : cb_fn(cb_arg, -ENOMEM);
3225 : 0 : return;
3226 : : }
3227 : : /* When freeing a cluster the flow should be (in order):
3228 : : * 1. Unmap the underlying area (so if the cluster is reclaimed in the future, it won't leak
3229 : : * old data)
3230 : : * 2. Once the unmap completes (to avoid any races with incoming writes that may claim the
3231 : : * cluster), update and sync metadata freeing the cluster
3232 : : * 3. Once metadata update is done, complete the user unmap request
3233 : : */
3234 : 120 : ctx->blob = blob;
3235 : 120 : ctx->page = cluster_start_page;
3236 : 120 : ctx->cluster_num = cluster_number;
3237 : 120 : ctx->md_page = bs_channel->new_cluster_page;
3238 : 120 : ctx->seq = bs_sequence_start_bs(_ch, &cpl);
3239 [ - + ]: 120 : if (!ctx->seq) {
3240 : 0 : free(ctx);
3241 : 0 : cb_fn(cb_arg, -ENOMEM);
3242 : 0 : return;
3243 : : }
3244 : :
3245 [ + + ]: 120 : if (blob->use_extent_table) {
3246 : 60 : ctx->extent_page = *bs_cluster_to_extent_page(blob, cluster_number);
3247 : : }
3248 : :
3249 : 120 : cpl.u.blob_basic.cb_fn = spdk_free_cluster_unmap_complete;
3250 : 120 : cpl.u.blob_basic.cb_arg = ctx;
3251 : : }
3252 : :
3253 : 194 : batch = bs_batch_open(_ch, &cpl, blob);
3254 [ - + ]: 194 : if (!batch) {
3255 : 0 : free(ctx);
3256 : 0 : cb_fn(cb_arg, -ENOMEM);
3257 : 0 : return;
3258 : : }
3259 : :
3260 [ + - ]: 194 : if (is_allocated) {
3261 : 194 : bs_batch_unmap_dev(batch, lba, lba_count);
3262 : : }
3263 : :
3264 : 194 : bs_batch_close(batch);
3265 : 194 : break;
3266 : : }
3267 : 0 : case SPDK_BLOB_READV:
3268 : : case SPDK_BLOB_WRITEV:
3269 : 0 : SPDK_ERRLOG("readv/write not valid\n");
3270 : 0 : cb_fn(cb_arg, -EINVAL);
3271 : 0 : break;
3272 : : }
3273 : 37830 : }
3274 : :
3275 : : static void
3276 : 77058 : blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel,
3277 : : void *payload, uint64_t offset, uint64_t length,
3278 : : spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
3279 : : {
3280 [ - + ]: 77058 : assert(blob != NULL);
3281 : :
3282 [ + + + + ]: 77058 : if (blob->data_ro && op_type != SPDK_BLOB_READ) {
3283 : 8 : cb_fn(cb_arg, -EPERM);
3284 : 8 : return;
3285 : : }
3286 : :
3287 [ + + ]: 77050 : if (length == 0) {
3288 : 984 : cb_fn(cb_arg, 0);
3289 : 984 : return;
3290 : : }
3291 : :
3292 [ + + ]: 76066 : if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
3293 : 48 : cb_fn(cb_arg, -EINVAL);
3294 : 48 : return;
3295 : : }
3296 [ + + ]: 76018 : if (length <= bs_num_io_units_to_cluster_boundary(blob, offset)) {
3297 : 75662 : blob_request_submit_op_single(_channel, blob, payload, offset, length,
3298 : : cb_fn, cb_arg, op_type);
3299 : : } else {
3300 : 356 : blob_request_submit_op_split(_channel, blob, payload, offset, length,
3301 : : cb_fn, cb_arg, op_type);
3302 : : }
3303 : : }
3304 : :
3305 : : struct rw_iov_ctx {
3306 : : struct spdk_blob *blob;
3307 : : struct spdk_io_channel *channel;
3308 : : spdk_blob_op_complete cb_fn;
3309 : : void *cb_arg;
3310 : : bool read;
3311 : : int iovcnt;
3312 : : struct iovec *orig_iov;
3313 : : uint64_t io_unit_offset;
3314 : : uint64_t io_units_remaining;
3315 : : uint64_t io_units_done;
3316 : : struct spdk_blob_ext_io_opts *ext_io_opts;
3317 : : struct iovec iov[0];
3318 : : };
3319 : :
3320 : : static void
3321 : 5652 : rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3322 : : {
3323 [ - + ]: 5652 : assert(cb_arg == NULL);
3324 : 5652 : bs_sequence_finish(seq, bserrno);
3325 : 5652 : }
3326 : :
3327 : : static void
3328 : 1488 : rw_iov_split_next(void *cb_arg, int bserrno)
3329 : : {
3330 : 1488 : struct rw_iov_ctx *ctx = cb_arg;
3331 : 1488 : struct spdk_blob *blob = ctx->blob;
3332 : : struct iovec *iov, *orig_iov;
3333 : : int iovcnt;
3334 : : size_t orig_iovoff;
3335 : : uint64_t io_units_count, io_units_to_boundary, io_unit_offset;
3336 : : uint64_t byte_count;
3337 : :
3338 [ + - + + ]: 1488 : if (bserrno != 0 || ctx->io_units_remaining == 0) {
3339 : 408 : ctx->cb_fn(ctx->cb_arg, bserrno);
3340 : 408 : free(ctx);
3341 : 408 : return;
3342 : : }
3343 : :
3344 : 1080 : io_unit_offset = ctx->io_unit_offset;
3345 : 1080 : io_units_to_boundary = bs_num_io_units_to_cluster_boundary(blob, io_unit_offset);
3346 : 1080 : io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary);
3347 : : /*
3348 : : * Get index and offset into the original iov array for our current position in the I/O sequence.
3349 : : * byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will
3350 : : * point to the current position in the I/O sequence.
3351 : : */
3352 : 1080 : byte_count = ctx->io_units_done * blob->bs->io_unit_size;
3353 : 1080 : orig_iov = &ctx->orig_iov[0];
3354 : 1080 : orig_iovoff = 0;
3355 [ + + ]: 2296 : while (byte_count > 0) {
3356 [ + + ]: 1216 : if (byte_count >= orig_iov->iov_len) {
3357 : 704 : byte_count -= orig_iov->iov_len;
3358 : 704 : orig_iov++;
3359 : : } else {
3360 : 512 : orig_iovoff = byte_count;
3361 : 512 : byte_count = 0;
3362 : : }
3363 : : }
3364 : :
3365 : : /*
3366 : : * Build an iov array for the next I/O in the sequence. byte_count will keep track of how many
3367 : : * bytes of this next I/O remain to be accounted for in the new iov array.
3368 : : */
3369 : 1080 : byte_count = io_units_count * blob->bs->io_unit_size;
3370 : 1080 : iov = &ctx->iov[0];
3371 : 1080 : iovcnt = 0;
3372 [ + + ]: 2760 : while (byte_count > 0) {
3373 [ - + ]: 1680 : assert(iovcnt < ctx->iovcnt);
3374 : 1680 : iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff);
3375 : 1680 : iov->iov_base = orig_iov->iov_base + orig_iovoff;
3376 : 1680 : byte_count -= iov->iov_len;
3377 : 1680 : orig_iovoff = 0;
3378 : 1680 : orig_iov++;
3379 : 1680 : iov++;
3380 : 1680 : iovcnt++;
3381 : : }
3382 : :
3383 : 1080 : ctx->io_unit_offset += io_units_count;
3384 : 1080 : ctx->io_units_remaining -= io_units_count;
3385 : 1080 : ctx->io_units_done += io_units_count;
3386 : 1080 : iov = &ctx->iov[0];
3387 : :
3388 [ + + ]: 1080 : if (ctx->read) {
3389 : 816 : spdk_blob_io_readv_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
3390 : : io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
3391 : : } else {
3392 : 264 : spdk_blob_io_writev_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
3393 : : io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
3394 : : }
3395 : : }
3396 : :
3397 : : static void
3398 : 6364 : blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel,
3399 : : struct iovec *iov, int iovcnt,
3400 : : uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg, bool read,
3401 : : struct spdk_blob_ext_io_opts *ext_io_opts)
3402 : : {
3403 : 5988 : struct spdk_bs_cpl cpl;
3404 : :
3405 [ - + ]: 6364 : assert(blob != NULL);
3406 : :
3407 [ + + + + ]: 6364 : if (!read && blob->data_ro) {
3408 : 8 : cb_fn(cb_arg, -EPERM);
3409 : 8 : return;
3410 : : }
3411 : :
3412 [ - + ]: 6356 : if (length == 0) {
3413 : 0 : cb_fn(cb_arg, 0);
3414 : 0 : return;
3415 : : }
3416 : :
3417 [ - + ]: 6356 : if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
3418 : 0 : cb_fn(cb_arg, -EINVAL);
3419 : 0 : return;
3420 : : }
3421 : :
3422 : : /*
3423 : : * For now, we implement readv/writev using a sequence (instead of a batch) to account for having
3424 : : * to split a request that spans a cluster boundary. For I/O that do not span a cluster boundary,
3425 : : * there will be no noticeable difference compared to using a batch. For I/O that do span a cluster
3426 : : * boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need
3427 : : * to allocate a separate iov array and split the I/O such that none of the resulting
3428 : : * smaller I/O cross a cluster boundary. These smaller I/O will be issued in sequence (not in parallel)
3429 : : * but since this case happens very infrequently, any performance impact will be negligible.
3430 : : *
3431 : : * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs
3432 : : * for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them
3433 : : * in a batch. That would also require creating an intermediate spdk_bs_cpl that would get called
3434 : : * when the batch was completed, to allow for freeing the memory for the iov arrays.
3435 : : */
3436 [ + + ]: 6356 : if (spdk_likely(length <= bs_num_io_units_to_cluster_boundary(blob, offset))) {
3437 : 5564 : uint64_t lba_count;
3438 : 5564 : uint64_t lba;
3439 : : bool is_allocated;
3440 : :
3441 : 5940 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3442 : 5940 : cpl.u.blob_basic.cb_fn = cb_fn;
3443 : 5940 : cpl.u.blob_basic.cb_arg = cb_arg;
3444 : :
3445 [ - + ]: 5940 : if (blob->frozen_refcnt) {
3446 : : /* This blob I/O is frozen */
3447 : : enum spdk_blob_op_type op_type;
3448 : : spdk_bs_user_op_t *op;
3449 : 0 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel);
3450 : :
3451 [ # # ]: 0 : op_type = read ? SPDK_BLOB_READV : SPDK_BLOB_WRITEV;
3452 : 0 : op = bs_user_op_alloc(_channel, &cpl, op_type, blob, iov, iovcnt, offset, length);
3453 [ # # ]: 0 : if (!op) {
3454 : 0 : cb_fn(cb_arg, -ENOMEM);
3455 : 0 : return;
3456 : : }
3457 : :
3458 : 0 : TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
3459 : :
3460 : 0 : return;
3461 : : }
3462 : :
3463 : 5940 : is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
3464 : :
3465 [ + + ]: 5940 : if (read) {
3466 : : spdk_bs_sequence_t *seq;
3467 : :
3468 : 4823 : seq = bs_sequence_start_blob(_channel, &cpl, blob);
3469 [ - + ]: 4823 : if (!seq) {
3470 : 0 : cb_fn(cb_arg, -ENOMEM);
3471 : 0 : return;
3472 : : }
3473 : :
3474 : 4823 : seq->ext_io_opts = ext_io_opts;
3475 : :
3476 [ + + ]: 4823 : if (is_allocated) {
3477 : 1699 : bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
3478 : : } else {
3479 : 3124 : bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count,
3480 : : rw_iov_done, NULL);
3481 : : }
3482 : : } else {
3483 [ + + ]: 1117 : if (is_allocated) {
3484 : : spdk_bs_sequence_t *seq;
3485 : :
3486 : 829 : seq = bs_sequence_start_blob(_channel, &cpl, blob);
3487 [ - + ]: 829 : if (!seq) {
3488 : 0 : cb_fn(cb_arg, -ENOMEM);
3489 : 0 : return;
3490 : : }
3491 : :
3492 : 829 : seq->ext_io_opts = ext_io_opts;
3493 : :
3494 : 829 : bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
3495 : : } else {
3496 : : /* Queue this operation and allocate the cluster */
3497 : : spdk_bs_user_op_t *op;
3498 : :
3499 : 288 : op = bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset,
3500 : : length);
3501 [ - + ]: 288 : if (!op) {
3502 : 0 : cb_fn(cb_arg, -ENOMEM);
3503 : 0 : return;
3504 : : }
3505 : :
3506 : 288 : op->ext_io_opts = ext_io_opts;
3507 : :
3508 : 288 : bs_allocate_and_copy_cluster(blob, _channel, offset, op);
3509 : : }
3510 : : }
3511 : : } else {
3512 : : struct rw_iov_ctx *ctx;
3513 : :
3514 : 416 : ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec));
3515 [ + + ]: 416 : if (ctx == NULL) {
3516 : 8 : cb_fn(cb_arg, -ENOMEM);
3517 : 8 : return;
3518 : : }
3519 : :
3520 : 408 : ctx->blob = blob;
3521 : 408 : ctx->channel = _channel;
3522 : 408 : ctx->cb_fn = cb_fn;
3523 : 408 : ctx->cb_arg = cb_arg;
3524 : 408 : ctx->read = read;
3525 : 408 : ctx->orig_iov = iov;
3526 : 408 : ctx->iovcnt = iovcnt;
3527 : 408 : ctx->io_unit_offset = offset;
3528 : 408 : ctx->io_units_remaining = length;
3529 : 408 : ctx->io_units_done = 0;
3530 : 408 : ctx->ext_io_opts = ext_io_opts;
3531 : :
3532 : 408 : rw_iov_split_next(ctx, 0);
3533 : : }
3534 : : }
3535 : :
3536 : : static struct spdk_blob *
3537 : 15624 : blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid)
3538 : : {
3539 : 15608 : struct spdk_blob find;
3540 : :
3541 [ + + ]: 15624 : if (spdk_bit_array_get(bs->open_blobids, blobid) == 0) {
3542 : 14052 : return NULL;
3543 : : }
3544 : :
3545 : 1572 : find.id = blobid;
3546 : 1572 : return RB_FIND(spdk_blob_tree, &bs->open_blobs, &find);
3547 : : }
3548 : :
3549 : : static void
3550 : 3608 : blob_get_snapshot_and_clone_entries(struct spdk_blob *blob,
3551 : : struct spdk_blob_list **snapshot_entry, struct spdk_blob_list **clone_entry)
3552 : : {
3553 [ - + ]: 3608 : assert(blob != NULL);
3554 : 3608 : *snapshot_entry = NULL;
3555 : 3608 : *clone_entry = NULL;
3556 : :
3557 [ + + ]: 3608 : if (blob->parent_id == SPDK_BLOBID_INVALID) {
3558 : 3044 : return;
3559 : : }
3560 : :
3561 [ + + ]: 852 : TAILQ_FOREACH(*snapshot_entry, &blob->bs->snapshots, link) {
3562 [ + + ]: 748 : if ((*snapshot_entry)->id == blob->parent_id) {
3563 : 460 : break;
3564 : : }
3565 : : }
3566 : :
3567 [ + + ]: 564 : if (*snapshot_entry != NULL) {
3568 [ + - ]: 550 : TAILQ_FOREACH(*clone_entry, &(*snapshot_entry)->clones, link) {
3569 [ + + ]: 550 : if ((*clone_entry)->id == blob->id) {
3570 : 460 : break;
3571 : : }
3572 : : }
3573 : :
3574 [ - + ]: 460 : assert(*clone_entry != NULL);
3575 : : }
3576 : : }
3577 : :
3578 : : static int
3579 : 3345 : bs_channel_create(void *io_device, void *ctx_buf)
3580 : : {
3581 : 3345 : struct spdk_blob_store *bs = io_device;
3582 : 3345 : struct spdk_bs_channel *channel = ctx_buf;
3583 : : struct spdk_bs_dev *dev;
3584 : 3345 : uint32_t max_ops = bs->max_channel_ops;
3585 : : uint32_t i;
3586 : :
3587 : 3345 : dev = bs->dev;
3588 : :
3589 : 3345 : channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set));
3590 [ - + ]: 3345 : if (!channel->req_mem) {
3591 : 0 : return -1;
3592 : : }
3593 : :
3594 : 3345 : TAILQ_INIT(&channel->reqs);
3595 : :
3596 [ + + ]: 1715985 : for (i = 0; i < max_ops; i++) {
3597 : 1712640 : TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
3598 : : }
3599 : :
3600 : 3345 : channel->bs = bs;
3601 : 3345 : channel->dev = dev;
3602 : 3345 : channel->dev_channel = dev->create_channel(dev);
3603 : :
3604 [ - + ]: 3345 : if (!channel->dev_channel) {
3605 : 0 : SPDK_ERRLOG("Failed to create device channel.\n");
3606 : 0 : free(channel->req_mem);
3607 : 0 : return -1;
3608 : : }
3609 : :
3610 : 3345 : channel->new_cluster_page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY,
3611 : : SPDK_MALLOC_DMA);
3612 [ - + ]: 3345 : if (!channel->new_cluster_page) {
3613 : 0 : SPDK_ERRLOG("Failed to allocate new cluster page\n");
3614 : 0 : free(channel->req_mem);
3615 : 0 : channel->dev->destroy_channel(channel->dev, channel->dev_channel);
3616 : 0 : return -1;
3617 : : }
3618 : :
3619 : 3345 : TAILQ_INIT(&channel->need_cluster_alloc);
3620 : 3345 : TAILQ_INIT(&channel->queued_io);
3621 : 3345 : RB_INIT(&channel->esnap_channels);
3622 : :
3623 : 3345 : return 0;
3624 : : }
3625 : :
3626 : : static void
3627 : 3345 : bs_channel_destroy(void *io_device, void *ctx_buf)
3628 : : {
3629 : 3345 : struct spdk_bs_channel *channel = ctx_buf;
3630 : : spdk_bs_user_op_t *op;
3631 : :
3632 [ - + ]: 3345 : while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) {
3633 : 0 : op = TAILQ_FIRST(&channel->need_cluster_alloc);
3634 [ # # ]: 0 : TAILQ_REMOVE(&channel->need_cluster_alloc, op, link);
3635 : 0 : bs_user_op_abort(op, -EIO);
3636 : : }
3637 : :
3638 [ - + ]: 3345 : while (!TAILQ_EMPTY(&channel->queued_io)) {
3639 : 0 : op = TAILQ_FIRST(&channel->queued_io);
3640 [ # # ]: 0 : TAILQ_REMOVE(&channel->queued_io, op, link);
3641 : 0 : bs_user_op_abort(op, -EIO);
3642 : : }
3643 : :
3644 : 3345 : blob_esnap_destroy_bs_channel(channel);
3645 : :
3646 : 3345 : free(channel->req_mem);
3647 : 3345 : spdk_free(channel->new_cluster_page);
3648 : 3345 : channel->dev->destroy_channel(channel->dev, channel->dev_channel);
3649 : 3345 : }
3650 : :
3651 : : static void
3652 : 3313 : bs_dev_destroy(void *io_device)
3653 : : {
3654 : 3313 : struct spdk_blob_store *bs = io_device;
3655 : : struct spdk_blob *blob, *blob_tmp;
3656 : :
3657 : 3313 : bs->dev->destroy(bs->dev);
3658 : :
3659 [ - + - - ]: 3313 : RB_FOREACH_SAFE(blob, spdk_blob_tree, &bs->open_blobs, blob_tmp) {
3660 : 0 : RB_REMOVE(spdk_blob_tree, &bs->open_blobs, blob);
3661 : 0 : spdk_bit_array_clear(bs->open_blobids, blob->id);
3662 : 0 : blob_free(blob);
3663 : : }
3664 : :
3665 : 3313 : spdk_spin_destroy(&bs->used_lock);
3666 : :
3667 : 3313 : spdk_bit_array_free(&bs->open_blobids);
3668 : 3313 : spdk_bit_array_free(&bs->used_blobids);
3669 : 3313 : spdk_bit_array_free(&bs->used_md_pages);
3670 : 3313 : spdk_bit_pool_free(&bs->used_clusters);
3671 : : /*
3672 : : * If this function is called for any reason except a successful unload,
3673 : : * the unload_cpl type will be NONE and this will be a nop.
3674 : : */
3675 : 3313 : bs_call_cpl(&bs->unload_cpl, bs->unload_err);
3676 : :
3677 : 3313 : free(bs);
3678 : 3313 : }
3679 : :
3680 : : static int
3681 : 1822 : bs_blob_list_add(struct spdk_blob *blob)
3682 : : {
3683 : : spdk_blob_id snapshot_id;
3684 : 1822 : struct spdk_blob_list *snapshot_entry = NULL;
3685 : 1822 : struct spdk_blob_list *clone_entry = NULL;
3686 : :
3687 [ - + ]: 1822 : assert(blob != NULL);
3688 : :
3689 : 1822 : snapshot_id = blob->parent_id;
3690 [ + + + + ]: 1822 : if (snapshot_id == SPDK_BLOBID_INVALID ||
3691 : : snapshot_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
3692 : 990 : return 0;
3693 : : }
3694 : :
3695 : 832 : snapshot_entry = bs_get_snapshot_entry(blob->bs, snapshot_id);
3696 [ + + ]: 832 : if (snapshot_entry == NULL) {
3697 : : /* Snapshot not found */
3698 : 572 : snapshot_entry = calloc(1, sizeof(struct spdk_blob_list));
3699 [ - + ]: 572 : if (snapshot_entry == NULL) {
3700 : 0 : return -ENOMEM;
3701 : : }
3702 : 572 : snapshot_entry->id = snapshot_id;
3703 : 572 : TAILQ_INIT(&snapshot_entry->clones);
3704 : 572 : TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link);
3705 : : } else {
3706 [ + + ]: 416 : TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
3707 [ - + ]: 156 : if (clone_entry->id == blob->id) {
3708 : 0 : break;
3709 : : }
3710 : : }
3711 : : }
3712 : :
3713 [ + - ]: 832 : if (clone_entry == NULL) {
3714 : : /* Clone not found */
3715 : 832 : clone_entry = calloc(1, sizeof(struct spdk_blob_list));
3716 [ - + ]: 832 : if (clone_entry == NULL) {
3717 : 0 : return -ENOMEM;
3718 : : }
3719 : 832 : clone_entry->id = blob->id;
3720 : 832 : TAILQ_INIT(&clone_entry->clones);
3721 : 832 : TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link);
3722 : 832 : snapshot_entry->clone_count++;
3723 : : }
3724 : :
3725 : 832 : return 0;
3726 : : }
3727 : :
3728 : : static void
3729 : 3452 : bs_blob_list_remove(struct spdk_blob *blob)
3730 : : {
3731 : 3452 : struct spdk_blob_list *snapshot_entry = NULL;
3732 : 3452 : struct spdk_blob_list *clone_entry = NULL;
3733 : :
3734 : 3452 : blob_get_snapshot_and_clone_entries(blob, &snapshot_entry, &clone_entry);
3735 : :
3736 [ + + ]: 3452 : if (snapshot_entry == NULL) {
3737 : 3024 : return;
3738 : : }
3739 : :
3740 : 428 : blob->parent_id = SPDK_BLOBID_INVALID;
3741 [ + + ]: 428 : TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
3742 : 428 : free(clone_entry);
3743 : :
3744 : 428 : snapshot_entry->clone_count--;
3745 : : }
3746 : :
3747 : : static int
3748 : 3313 : bs_blob_list_free(struct spdk_blob_store *bs)
3749 : : {
3750 : : struct spdk_blob_list *snapshot_entry;
3751 : : struct spdk_blob_list *snapshot_entry_tmp;
3752 : : struct spdk_blob_list *clone_entry;
3753 : : struct spdk_blob_list *clone_entry_tmp;
3754 : :
3755 [ + + ]: 3603 : TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) {
3756 [ + + ]: 598 : TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) {
3757 [ + + ]: 308 : TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
3758 : 308 : free(clone_entry);
3759 : : }
3760 [ + + ]: 290 : TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link);
3761 : 290 : free(snapshot_entry);
3762 : : }
3763 : :
3764 : 3313 : return 0;
3765 : : }
3766 : :
3767 : : static void
3768 : 3313 : bs_free(struct spdk_blob_store *bs)
3769 : : {
3770 : 3313 : bs_blob_list_free(bs);
3771 : :
3772 : 3313 : bs_unregister_md_thread(bs);
3773 : 3313 : spdk_io_device_unregister(bs, bs_dev_destroy);
3774 : 3313 : }
3775 : :
3776 : : void
3777 : 5618 : spdk_bs_opts_init(struct spdk_bs_opts *opts, size_t opts_size)
3778 : : {
3779 : :
3780 [ - + ]: 5618 : if (!opts) {
3781 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
3782 : 0 : return;
3783 : : }
3784 : :
3785 [ - + ]: 5618 : if (!opts_size) {
3786 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
3787 : 0 : return;
3788 : : }
3789 : :
3790 : 5618 : memset(opts, 0, opts_size);
3791 : 5618 : opts->opts_size = opts_size;
3792 : :
3793 : : #define FIELD_OK(field) \
3794 : : offsetof(struct spdk_bs_opts, field) + sizeof(opts->field) <= opts_size
3795 : :
3796 : : #define SET_FIELD(field, value) \
3797 : : if (FIELD_OK(field)) { \
3798 : : opts->field = value; \
3799 : : } \
3800 : :
3801 [ + - ]: 5618 : SET_FIELD(cluster_sz, SPDK_BLOB_OPTS_CLUSTER_SZ);
3802 [ + - ]: 5618 : SET_FIELD(num_md_pages, SPDK_BLOB_OPTS_NUM_MD_PAGES);
3803 [ + - ]: 5618 : SET_FIELD(max_md_ops, SPDK_BLOB_OPTS_NUM_MD_PAGES);
3804 [ + - ]: 5618 : SET_FIELD(max_channel_ops, SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS);
3805 [ + - ]: 5618 : SET_FIELD(clear_method, BS_CLEAR_WITH_UNMAP);
3806 : :
3807 [ + - ]: 5618 : if (FIELD_OK(bstype)) {
3808 : 5618 : memset(&opts->bstype, 0, sizeof(opts->bstype));
3809 : : }
3810 : :
3811 [ + - ]: 5618 : SET_FIELD(iter_cb_fn, NULL);
3812 [ + - ]: 5618 : SET_FIELD(iter_cb_arg, NULL);
3813 [ + - ]: 5618 : SET_FIELD(force_recover, false);
3814 [ + - ]: 5618 : SET_FIELD(esnap_bs_dev_create, NULL);
3815 [ + - ]: 5618 : SET_FIELD(esnap_ctx, NULL);
3816 : :
3817 : : #undef FIELD_OK
3818 : : #undef SET_FIELD
3819 : : }
3820 : :
3821 : : static int
3822 : 977 : bs_opts_verify(struct spdk_bs_opts *opts)
3823 : : {
3824 [ + + + - : 977 : if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 ||
+ - ]
3825 [ - + ]: 969 : opts->max_channel_ops == 0) {
3826 : 8 : SPDK_ERRLOG("Blobstore options cannot be set to 0\n");
3827 : 8 : return -1;
3828 : : }
3829 : :
3830 : 969 : return 0;
3831 : : }
3832 : :
3833 : : /* START spdk_bs_load */
3834 : :
3835 : : /* spdk_bs_load_ctx is used for init, load, unload and dump code paths. */
3836 : :
3837 : : struct spdk_bs_load_ctx {
3838 : : struct spdk_blob_store *bs;
3839 : : struct spdk_bs_super_block *super;
3840 : :
3841 : : struct spdk_bs_md_mask *mask;
3842 : : bool in_page_chain;
3843 : : uint32_t page_index;
3844 : : uint32_t cur_page;
3845 : : struct spdk_blob_md_page *page;
3846 : :
3847 : : uint64_t num_extent_pages;
3848 : : uint32_t *extent_page_num;
3849 : : struct spdk_blob_md_page *extent_pages;
3850 : : struct spdk_bit_array *used_clusters;
3851 : :
3852 : : spdk_bs_sequence_t *seq;
3853 : : spdk_blob_op_with_handle_complete iter_cb_fn;
3854 : : void *iter_cb_arg;
3855 : : struct spdk_blob *blob;
3856 : : spdk_blob_id blobid;
3857 : :
3858 : : bool force_recover;
3859 : :
3860 : : /* These fields are used in the spdk_bs_dump path. */
3861 : : bool dumping;
3862 : : FILE *fp;
3863 : : spdk_bs_dump_print_xattr print_xattr_fn;
3864 : : char xattr_name[4096];
3865 : : };
3866 : :
3867 : : static int
3868 : 3329 : bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs,
3869 : : struct spdk_bs_load_ctx **_ctx)
3870 : : {
3871 : : struct spdk_blob_store *bs;
3872 : : struct spdk_bs_load_ctx *ctx;
3873 : : uint64_t dev_size;
3874 : : int rc;
3875 : :
3876 : 3329 : dev_size = dev->blocklen * dev->blockcnt;
3877 [ + + ]: 3329 : if (dev_size < opts->cluster_sz) {
3878 : : /* Device size cannot be smaller than cluster size of blobstore */
3879 [ - + ]: 8 : SPDK_INFOLOG(blob, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n",
3880 : : dev_size, opts->cluster_sz);
3881 : 8 : return -ENOSPC;
3882 : : }
3883 [ + + ]: 3321 : if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) {
3884 : : /* Cluster size cannot be smaller than page size */
3885 : 8 : SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n",
3886 : : opts->cluster_sz, SPDK_BS_PAGE_SIZE);
3887 : 8 : return -EINVAL;
3888 : : }
3889 : 3313 : bs = calloc(1, sizeof(struct spdk_blob_store));
3890 [ - + ]: 3313 : if (!bs) {
3891 : 0 : return -ENOMEM;
3892 : : }
3893 : :
3894 : 3313 : ctx = calloc(1, sizeof(struct spdk_bs_load_ctx));
3895 [ - + ]: 3313 : if (!ctx) {
3896 : 0 : free(bs);
3897 : 0 : return -ENOMEM;
3898 : : }
3899 : :
3900 : 3313 : ctx->bs = bs;
3901 : 3313 : ctx->iter_cb_fn = opts->iter_cb_fn;
3902 : 3313 : ctx->iter_cb_arg = opts->iter_cb_arg;
3903 : 3313 : ctx->force_recover = opts->force_recover;
3904 : :
3905 : 3313 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
3906 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
3907 [ - + ]: 3313 : if (!ctx->super) {
3908 : 0 : free(ctx);
3909 : 0 : free(bs);
3910 : 0 : return -ENOMEM;
3911 : : }
3912 : :
3913 : 3313 : RB_INIT(&bs->open_blobs);
3914 : 3313 : TAILQ_INIT(&bs->snapshots);
3915 : 3313 : bs->dev = dev;
3916 : 3313 : bs->md_thread = spdk_get_thread();
3917 [ - + ]: 3313 : assert(bs->md_thread != NULL);
3918 : :
3919 : : /*
3920 : : * Do not use bs_lba_to_cluster() here since blockcnt may not be an
3921 : : * even multiple of the cluster size.
3922 : : */
3923 : 3313 : bs->cluster_sz = opts->cluster_sz;
3924 : 3313 : bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
3925 : 3313 : ctx->used_clusters = spdk_bit_array_create(bs->total_clusters);
3926 [ - + ]: 3313 : if (!ctx->used_clusters) {
3927 : 0 : spdk_free(ctx->super);
3928 : 0 : free(ctx);
3929 : 0 : free(bs);
3930 : 0 : return -ENOMEM;
3931 : : }
3932 : :
3933 : 3313 : bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
3934 [ + - ]: 3313 : if (spdk_u32_is_pow2(bs->pages_per_cluster)) {
3935 : 3313 : bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster);
3936 : : }
3937 : 3313 : bs->num_free_clusters = bs->total_clusters;
3938 : 3313 : bs->io_unit_size = dev->blocklen;
3939 : :
3940 : 3313 : bs->max_channel_ops = opts->max_channel_ops;
3941 : 3313 : bs->super_blob = SPDK_BLOBID_INVALID;
3942 : 3313 : memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
3943 : 3313 : bs->esnap_bs_dev_create = opts->esnap_bs_dev_create;
3944 : 3313 : bs->esnap_ctx = opts->esnap_ctx;
3945 : :
3946 : : /* The metadata is assumed to be at least 1 page */
3947 : 3313 : bs->used_md_pages = spdk_bit_array_create(1);
3948 : 3313 : bs->used_blobids = spdk_bit_array_create(0);
3949 : 3313 : bs->open_blobids = spdk_bit_array_create(0);
3950 : :
3951 : 3313 : spdk_spin_init(&bs->used_lock);
3952 : :
3953 : 3313 : spdk_io_device_register(bs, bs_channel_create, bs_channel_destroy,
3954 : : sizeof(struct spdk_bs_channel), "blobstore");
3955 : 3313 : rc = bs_register_md_thread(bs);
3956 [ - + ]: 3313 : if (rc == -1) {
3957 : 0 : spdk_io_device_unregister(bs, NULL);
3958 : 0 : spdk_spin_destroy(&bs->used_lock);
3959 : 0 : spdk_bit_array_free(&bs->open_blobids);
3960 : 0 : spdk_bit_array_free(&bs->used_blobids);
3961 : 0 : spdk_bit_array_free(&bs->used_md_pages);
3962 : 0 : spdk_bit_array_free(&ctx->used_clusters);
3963 : 0 : spdk_free(ctx->super);
3964 : 0 : free(ctx);
3965 : 0 : free(bs);
3966 : : /* FIXME: this is a lie but don't know how to get a proper error code here */
3967 : 0 : return -ENOMEM;
3968 : : }
3969 : :
3970 : 3313 : *_ctx = ctx;
3971 : 3313 : *_bs = bs;
3972 : 3313 : return 0;
3973 : : }
3974 : :
3975 : : static void
3976 : 1786 : bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno)
3977 : : {
3978 [ - + ]: 1786 : assert(bserrno != 0);
3979 : :
3980 : 1786 : spdk_free(ctx->super);
3981 : 1786 : bs_sequence_finish(ctx->seq, bserrno);
3982 : 1786 : bs_free(ctx->bs);
3983 : 1786 : spdk_bit_array_free(&ctx->used_clusters);
3984 : 1786 : free(ctx);
3985 : 1786 : }
3986 : :
3987 : : static void
3988 : 1674 : bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
3989 : : struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
3990 : : {
3991 : : /* Update the values in the super block */
3992 : 1674 : super->super_blob = bs->super_blob;
3993 : 1674 : memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
3994 : 1674 : super->crc = blob_md_page_calc_crc(super);
3995 : 1674 : bs_sequence_write_dev(seq, super, bs_page_to_lba(bs, 0),
3996 : 1674 : bs_byte_to_lba(bs, sizeof(*super)),
3997 : : cb_fn, cb_arg);
3998 : 1674 : }
3999 : :
4000 : : static void
4001 : 1533 : bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
4002 : : {
4003 : 1533 : struct spdk_bs_load_ctx *ctx = arg;
4004 : : uint64_t mask_size, lba, lba_count;
4005 : :
4006 : : /* Write out the used clusters mask */
4007 : 1533 : mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
4008 : 1533 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
4009 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4010 [ - + ]: 1533 : if (!ctx->mask) {
4011 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4012 : 0 : return;
4013 : : }
4014 : :
4015 : 1533 : ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
4016 : 1533 : ctx->mask->length = ctx->bs->total_clusters;
4017 : : /* We could get here through the normal unload path, or through dirty
4018 : : * shutdown recovery. For the normal unload path, we use the mask from
4019 : : * the bit pool. For dirty shutdown recovery, we don't have a bit pool yet -
4020 : : * only the bit array from the load ctx.
4021 : : */
4022 [ + + ]: 1533 : if (ctx->bs->used_clusters) {
4023 [ - + ]: 1321 : assert(ctx->mask->length == spdk_bit_pool_capacity(ctx->bs->used_clusters));
4024 : 1321 : spdk_bit_pool_store_mask(ctx->bs->used_clusters, ctx->mask->mask);
4025 : : } else {
4026 [ - + ]: 212 : assert(ctx->mask->length == spdk_bit_array_capacity(ctx->used_clusters));
4027 : 212 : spdk_bit_array_store_mask(ctx->used_clusters, ctx->mask->mask);
4028 : : }
4029 : 1533 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
4030 : 1533 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
4031 : 1533 : bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
4032 : : }
4033 : :
4034 : : static void
4035 : 1533 : bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
4036 : : {
4037 : 1533 : struct spdk_bs_load_ctx *ctx = arg;
4038 : : uint64_t mask_size, lba, lba_count;
4039 : :
4040 : 1533 : mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
4041 : 1533 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
4042 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4043 [ - + ]: 1533 : if (!ctx->mask) {
4044 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4045 : 0 : return;
4046 : : }
4047 : :
4048 : 1533 : ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
4049 : 1533 : ctx->mask->length = ctx->super->md_len;
4050 [ - + ]: 1533 : assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
4051 : :
4052 : 1533 : spdk_bit_array_store_mask(ctx->bs->used_md_pages, ctx->mask->mask);
4053 : 1533 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
4054 : 1533 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
4055 : 1533 : bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
4056 : : }
4057 : :
4058 : : static void
4059 : 1533 : bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
4060 : : {
4061 : 1533 : struct spdk_bs_load_ctx *ctx = arg;
4062 : : uint64_t mask_size, lba, lba_count;
4063 : :
4064 [ + + ]: 1533 : if (ctx->super->used_blobid_mask_len == 0) {
4065 : : /*
4066 : : * This is a pre-v3 on-disk format where the blobid mask does not get
4067 : : * written to disk.
4068 : : */
4069 : 48 : cb_fn(seq, arg, 0);
4070 : 48 : return;
4071 : : }
4072 : :
4073 : 1485 : mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
4074 : 1485 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
4075 : : SPDK_MALLOC_DMA);
4076 [ - + ]: 1485 : if (!ctx->mask) {
4077 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4078 : 0 : return;
4079 : : }
4080 : :
4081 : 1485 : ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS;
4082 : 1485 : ctx->mask->length = ctx->super->md_len;
4083 [ - + ]: 1485 : assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids));
4084 : :
4085 : 1485 : spdk_bit_array_store_mask(ctx->bs->used_blobids, ctx->mask->mask);
4086 : 1485 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
4087 : 1485 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
4088 : 1485 : bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
4089 : : }
4090 : :
4091 : : static void
4092 : 1402 : blob_set_thin_provision(struct spdk_blob *blob)
4093 : : {
4094 : 1402 : blob_verify_md_op(blob);
4095 : 1402 : blob->invalid_flags |= SPDK_BLOB_THIN_PROV;
4096 : 1402 : blob->state = SPDK_BLOB_STATE_DIRTY;
4097 : 1402 : }
4098 : :
4099 : : static void
4100 : 4198 : blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method)
4101 : : {
4102 : 4198 : blob_verify_md_op(blob);
4103 : 4198 : blob->clear_method = clear_method;
4104 : 4198 : blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT);
4105 : 4198 : blob->state = SPDK_BLOB_STATE_DIRTY;
4106 : 4198 : }
4107 : :
4108 : : static void bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno);
4109 : :
4110 : : static void
4111 : 48 : bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno)
4112 : : {
4113 : 48 : struct spdk_bs_load_ctx *ctx = cb_arg;
4114 : : spdk_blob_id id;
4115 : : int64_t page_num;
4116 : :
4117 : : /* Iterate to next blob (we can't use spdk_bs_iter_next function as our
4118 : : * last blob has been removed */
4119 : 48 : page_num = bs_blobid_to_page(ctx->blobid);
4120 : 48 : page_num++;
4121 : 48 : page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num);
4122 [ + - ]: 48 : if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) {
4123 : 48 : bs_load_iter(ctx, NULL, -ENOENT);
4124 : 48 : return;
4125 : : }
4126 : :
4127 : 0 : id = bs_page_to_blobid(page_num);
4128 : :
4129 : 0 : spdk_bs_open_blob(ctx->bs, id, bs_load_iter, ctx);
4130 : : }
4131 : :
4132 : : static void
4133 : 48 : bs_delete_corrupted_close_cb(void *cb_arg, int bserrno)
4134 : : {
4135 : 48 : struct spdk_bs_load_ctx *ctx = cb_arg;
4136 : :
4137 [ - + ]: 48 : if (bserrno != 0) {
4138 : 0 : SPDK_ERRLOG("Failed to close corrupted blob\n");
4139 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4140 : 0 : return;
4141 : : }
4142 : :
4143 : 48 : spdk_bs_delete_blob(ctx->bs, ctx->blobid, bs_delete_corrupted_blob_cpl, ctx);
4144 : : }
4145 : :
4146 : : static void
4147 : 48 : bs_delete_corrupted_blob(void *cb_arg, int bserrno)
4148 : : {
4149 : 48 : struct spdk_bs_load_ctx *ctx = cb_arg;
4150 : : uint64_t i;
4151 : :
4152 [ - + ]: 48 : if (bserrno != 0) {
4153 : 0 : SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
4154 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4155 : 0 : return;
4156 : : }
4157 : :
4158 : : /* Snapshot and clone have the same copy of cluster map and extent pages
4159 : : * at this point. Let's clear both for snapshot now,
4160 : : * so that it won't be cleared for clone later when we remove snapshot.
4161 : : * Also set thin provision to pass data corruption check */
4162 [ + + ]: 528 : for (i = 0; i < ctx->blob->active.num_clusters; i++) {
4163 : 480 : ctx->blob->active.clusters[i] = 0;
4164 : : }
4165 [ + + ]: 72 : for (i = 0; i < ctx->blob->active.num_extent_pages; i++) {
4166 : 24 : ctx->blob->active.extent_pages[i] = 0;
4167 : : }
4168 : :
4169 : 48 : ctx->blob->active.num_allocated_clusters = 0;
4170 : :
4171 : 48 : ctx->blob->md_ro = false;
4172 : :
4173 : 48 : blob_set_thin_provision(ctx->blob);
4174 : :
4175 : 48 : ctx->blobid = ctx->blob->id;
4176 : :
4177 : 48 : spdk_blob_close(ctx->blob, bs_delete_corrupted_close_cb, ctx);
4178 : : }
4179 : :
4180 : : static void
4181 : 24 : bs_update_corrupted_blob(void *cb_arg, int bserrno)
4182 : : {
4183 : 24 : struct spdk_bs_load_ctx *ctx = cb_arg;
4184 : :
4185 [ - + ]: 24 : if (bserrno != 0) {
4186 : 0 : SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
4187 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4188 : 0 : return;
4189 : : }
4190 : :
4191 : 24 : ctx->blob->md_ro = false;
4192 : 24 : blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true);
4193 : 24 : blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true);
4194 : 24 : spdk_blob_set_read_only(ctx->blob);
4195 : :
4196 [ - + ]: 24 : if (ctx->iter_cb_fn) {
4197 : 0 : ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0);
4198 : : }
4199 : 24 : bs_blob_list_add(ctx->blob);
4200 : :
4201 : 24 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4202 : : }
4203 : :
4204 : : static void
4205 : 72 : bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno)
4206 : : {
4207 : 72 : struct spdk_bs_load_ctx *ctx = cb_arg;
4208 : :
4209 [ - + ]: 72 : if (bserrno != 0) {
4210 : 0 : SPDK_ERRLOG("Failed to open clone of a corrupted blob\n");
4211 : 0 : spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
4212 : 0 : return;
4213 : : }
4214 : :
4215 [ + + ]: 72 : if (blob->parent_id == ctx->blob->id) {
4216 : : /* Power failure occurred before updating clone (snapshot delete case)
4217 : : * or after updating clone (creating snapshot case) - keep snapshot */
4218 : 24 : spdk_blob_close(blob, bs_update_corrupted_blob, ctx);
4219 : : } else {
4220 : : /* Power failure occurred after updating clone (snapshot delete case)
4221 : : * or before updating clone (creating snapshot case) - remove snapshot */
4222 : 48 : spdk_blob_close(blob, bs_delete_corrupted_blob, ctx);
4223 : : }
4224 : : }
4225 : :
4226 : : static void
4227 : 1462 : bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno)
4228 : : {
4229 : 1462 : struct spdk_bs_load_ctx *ctx = arg;
4230 : 1462 : const void *value;
4231 : 1462 : size_t len;
4232 : 1462 : int rc = 0;
4233 : :
4234 [ + + ]: 1462 : if (bserrno == 0) {
4235 : : /* Examine blob if it is corrupted after power failure. Fix
4236 : : * the ones that can be fixed and remove any other corrupted
4237 : : * ones. If it is not corrupted just process it */
4238 : 896 : rc = blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true);
4239 [ + + ]: 896 : if (rc != 0) {
4240 : 856 : rc = blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true);
4241 [ + + ]: 856 : if (rc != 0) {
4242 : : /* Not corrupted - process it and continue with iterating through blobs */
4243 [ + + ]: 824 : if (ctx->iter_cb_fn) {
4244 : 68 : ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0);
4245 : : }
4246 : 824 : bs_blob_list_add(blob);
4247 : 824 : spdk_bs_iter_next(ctx->bs, blob, bs_load_iter, ctx);
4248 : 824 : return;
4249 : : }
4250 : :
4251 : : }
4252 : :
4253 [ - + ]: 72 : assert(len == sizeof(spdk_blob_id));
4254 : :
4255 : 72 : ctx->blob = blob;
4256 : :
4257 : : /* Open clone to check if we are able to fix this blob or should we remove it */
4258 : 72 : spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, bs_examine_clone, ctx);
4259 : 72 : return;
4260 [ + - ]: 566 : } else if (bserrno == -ENOENT) {
4261 : 566 : bserrno = 0;
4262 : : } else {
4263 : : /*
4264 : : * This case needs to be looked at further. Same problem
4265 : : * exists with applications that rely on explicit blob
4266 : : * iteration. We should just skip the blob that failed
4267 : : * to load and continue on to the next one.
4268 : : */
4269 : 0 : SPDK_ERRLOG("Error in iterating blobs\n");
4270 : : }
4271 : :
4272 : 566 : ctx->iter_cb_fn = NULL;
4273 : :
4274 : 566 : spdk_free(ctx->super);
4275 : 566 : spdk_free(ctx->mask);
4276 : 566 : bs_sequence_finish(ctx->seq, bserrno);
4277 : 566 : free(ctx);
4278 : : }
4279 : :
4280 : : static void bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg);
4281 : :
4282 : : static void
4283 : 566 : bs_load_complete(struct spdk_bs_load_ctx *ctx)
4284 : : {
4285 : 566 : ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
4286 [ - + ]: 566 : if (ctx->dumping) {
4287 : 0 : bs_dump_read_md_page(ctx->seq, ctx);
4288 : 0 : return;
4289 : : }
4290 : 566 : spdk_bs_iter_first(ctx->bs, bs_load_iter, ctx);
4291 : : }
4292 : :
4293 : : static void
4294 : 354 : bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4295 : : {
4296 : 354 : struct spdk_bs_load_ctx *ctx = cb_arg;
4297 : : int rc;
4298 : :
4299 : : /* The type must be correct */
4300 [ - + ]: 354 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS);
4301 : :
4302 : : /* The length of the mask (in bits) must not be greater than
4303 : : * the length of the buffer (converted to bits) */
4304 [ - + ]: 354 : assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8));
4305 : :
4306 : : /* The length of the mask must be exactly equal to the size
4307 : : * (in pages) of the metadata region */
4308 [ - + ]: 354 : assert(ctx->mask->length == ctx->super->md_len);
4309 : :
4310 : 354 : rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length);
4311 [ - + ]: 354 : if (rc < 0) {
4312 : 0 : spdk_free(ctx->mask);
4313 : 0 : bs_load_ctx_fail(ctx, rc);
4314 : 0 : return;
4315 : : }
4316 : :
4317 : 354 : spdk_bit_array_load_mask(ctx->bs->used_blobids, ctx->mask->mask);
4318 : 354 : bs_load_complete(ctx);
4319 : : }
4320 : :
4321 : : static void
4322 : 354 : bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4323 : : {
4324 : 354 : struct spdk_bs_load_ctx *ctx = cb_arg;
4325 : : uint64_t lba, lba_count, mask_size;
4326 : : int rc;
4327 : :
4328 [ - + ]: 354 : if (bserrno != 0) {
4329 : 0 : bs_load_ctx_fail(ctx, bserrno);
4330 : 0 : return;
4331 : : }
4332 : :
4333 : : /* The type must be correct */
4334 [ - + ]: 354 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
4335 : : /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
4336 [ - + ]: 354 : assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
4337 : : struct spdk_blob_md_page) * 8));
4338 : : /*
4339 : : * The length of the mask must be equal to or larger than the total number of clusters. It may be
4340 : : * larger than the total number of clusters due to a failure spdk_bs_grow.
4341 : : */
4342 [ - + ]: 354 : assert(ctx->mask->length >= ctx->bs->total_clusters);
4343 [ + + ]: 354 : if (ctx->mask->length > ctx->bs->total_clusters) {
4344 : 8 : SPDK_WARNLOG("Shrink the used_custers mask length to total_clusters");
4345 : 8 : ctx->mask->length = ctx->bs->total_clusters;
4346 : : }
4347 : :
4348 : 354 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->mask->length);
4349 [ - + ]: 354 : if (rc < 0) {
4350 : 0 : spdk_free(ctx->mask);
4351 : 0 : bs_load_ctx_fail(ctx, rc);
4352 : 0 : return;
4353 : : }
4354 : :
4355 : 354 : spdk_bit_array_load_mask(ctx->used_clusters, ctx->mask->mask);
4356 : 354 : ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->used_clusters);
4357 [ - + ]: 354 : assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
4358 : :
4359 : 354 : spdk_free(ctx->mask);
4360 : :
4361 : : /* Read the used blobids mask */
4362 : 354 : mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
4363 : 354 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
4364 : : SPDK_MALLOC_DMA);
4365 [ - + ]: 354 : if (!ctx->mask) {
4366 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4367 : 0 : return;
4368 : : }
4369 : 354 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
4370 : 354 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
4371 : 354 : bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
4372 : : bs_load_used_blobids_cpl, ctx);
4373 : : }
4374 : :
4375 : : static void
4376 : 354 : bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4377 : : {
4378 : 354 : struct spdk_bs_load_ctx *ctx = cb_arg;
4379 : : uint64_t lba, lba_count, mask_size;
4380 : : int rc;
4381 : :
4382 [ - + ]: 354 : if (bserrno != 0) {
4383 : 0 : bs_load_ctx_fail(ctx, bserrno);
4384 : 0 : return;
4385 : : }
4386 : :
4387 : : /* The type must be correct */
4388 [ - + ]: 354 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
4389 : : /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
4390 [ - + ]: 354 : assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
4391 : : 8));
4392 : : /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
4393 [ - + ]: 354 : if (ctx->mask->length != ctx->super->md_len) {
4394 : 0 : SPDK_ERRLOG("mismatched md_len in used_pages mask: "
4395 : : "mask->length=%" PRIu32 " super->md_len=%" PRIu32 "\n",
4396 : : ctx->mask->length, ctx->super->md_len);
4397 : 0 : assert(false);
4398 : : }
4399 : :
4400 : 354 : rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length);
4401 [ - + ]: 354 : if (rc < 0) {
4402 : 0 : spdk_free(ctx->mask);
4403 : 0 : bs_load_ctx_fail(ctx, rc);
4404 : 0 : return;
4405 : : }
4406 : :
4407 : 354 : spdk_bit_array_load_mask(ctx->bs->used_md_pages, ctx->mask->mask);
4408 : 354 : spdk_free(ctx->mask);
4409 : :
4410 : : /* Read the used clusters mask */
4411 : 354 : mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
4412 : 354 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
4413 : : SPDK_MALLOC_DMA);
4414 [ - + ]: 354 : if (!ctx->mask) {
4415 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4416 : 0 : return;
4417 : : }
4418 : 354 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
4419 : 354 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
4420 : 354 : bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
4421 : : bs_load_used_clusters_cpl, ctx);
4422 : : }
4423 : :
4424 : : static void
4425 : 354 : bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx)
4426 : : {
4427 : : uint64_t lba, lba_count, mask_size;
4428 : :
4429 : : /* Read the used pages mask */
4430 : 354 : mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
4431 : 354 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
4432 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4433 [ - + ]: 354 : if (!ctx->mask) {
4434 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4435 : 0 : return;
4436 : : }
4437 : :
4438 : 354 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
4439 : 354 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
4440 : 354 : bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
4441 : : bs_load_used_pages_cpl, ctx);
4442 : : }
4443 : :
4444 : : static int
4445 : 492 : bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page)
4446 : : {
4447 : 492 : struct spdk_blob_store *bs = ctx->bs;
4448 : : struct spdk_blob_md_descriptor *desc;
4449 : 492 : size_t cur_desc = 0;
4450 : :
4451 : 492 : desc = (struct spdk_blob_md_descriptor *)page->descriptors;
4452 [ + - ]: 1436 : while (cur_desc < sizeof(page->descriptors)) {
4453 [ + + ]: 1436 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
4454 [ + - ]: 452 : if (desc->length == 0) {
4455 : : /* If padding and length are 0, this terminates the page */
4456 : 452 : break;
4457 : : }
4458 [ + + ]: 984 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
4459 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
4460 : : unsigned int i, j;
4461 : 136 : unsigned int cluster_count = 0;
4462 : : uint32_t cluster_idx;
4463 : :
4464 : 136 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
4465 : :
4466 [ + + ]: 272 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
4467 [ + + ]: 1656 : for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
4468 : 1520 : cluster_idx = desc_extent_rle->extents[i].cluster_idx;
4469 : : /*
4470 : : * cluster_idx = 0 means an unallocated cluster - don't mark that
4471 : : * in the used cluster map.
4472 : : */
4473 [ + + ]: 1520 : if (cluster_idx != 0) {
4474 : 1080 : SPDK_NOTICELOG("Recover: cluster %" PRIu32 "\n", cluster_idx + j);
4475 : 1080 : spdk_bit_array_set(ctx->used_clusters, cluster_idx + j);
4476 [ - + ]: 1080 : if (bs->num_free_clusters == 0) {
4477 : 0 : return -ENOSPC;
4478 : : }
4479 : 1080 : bs->num_free_clusters--;
4480 : : }
4481 : 1520 : cluster_count++;
4482 : : }
4483 : : }
4484 [ - + ]: 136 : if (cluster_count == 0) {
4485 : 0 : return -EINVAL;
4486 : : }
4487 [ + + ]: 848 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
4488 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
4489 : : uint32_t i;
4490 : 104 : uint32_t cluster_count = 0;
4491 : : uint32_t cluster_idx;
4492 : : size_t cluster_idx_length;
4493 : :
4494 : 104 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
4495 : 104 : cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
4496 : :
4497 [ + - ]: 104 : if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
4498 [ - + ]: 104 : (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
4499 : 0 : return -EINVAL;
4500 : : }
4501 : :
4502 [ + + ]: 1304 : for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
4503 : 1200 : cluster_idx = desc_extent->cluster_idx[i];
4504 : : /*
4505 : : * cluster_idx = 0 means an unallocated cluster - don't mark that
4506 : : * in the used cluster map.
4507 : : */
4508 [ + - ]: 1200 : if (cluster_idx != 0) {
4509 [ - + ]: 1200 : if (cluster_idx < desc_extent->start_cluster_idx &&
4510 [ # # ]: 0 : cluster_idx >= desc_extent->start_cluster_idx + cluster_count) {
4511 : 0 : return -EINVAL;
4512 : : }
4513 : 1200 : spdk_bit_array_set(ctx->used_clusters, cluster_idx);
4514 [ - + ]: 1200 : if (bs->num_free_clusters == 0) {
4515 : 0 : return -ENOSPC;
4516 : : }
4517 : 1200 : bs->num_free_clusters--;
4518 : : }
4519 : 1200 : cluster_count++;
4520 : : }
4521 : :
4522 [ - + ]: 104 : if (cluster_count == 0) {
4523 : 0 : return -EINVAL;
4524 : : }
4525 [ + + ]: 744 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
4526 : : /* Skip this item */
4527 [ + + ]: 592 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
4528 : : /* Skip this item */
4529 [ + + ]: 472 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
4530 : : /* Skip this item */
4531 [ + - ]: 164 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
4532 : : struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
4533 : 164 : uint32_t num_extent_pages = ctx->num_extent_pages;
4534 : : uint32_t i;
4535 : : size_t extent_pages_length;
4536 : : void *tmp;
4537 : :
4538 : 164 : desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
4539 : 164 : extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
4540 : :
4541 [ + - ]: 164 : if (desc_extent_table->length == 0 ||
4542 [ - + ]: 164 : (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
4543 : 0 : return -EINVAL;
4544 : : }
4545 : :
4546 [ + + ]: 320 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
4547 [ + + ]: 156 : if (desc_extent_table->extent_page[i].page_idx != 0) {
4548 [ - + ]: 104 : if (desc_extent_table->extent_page[i].num_pages != 1) {
4549 : 0 : return -EINVAL;
4550 : : }
4551 : 104 : num_extent_pages += 1;
4552 : : }
4553 : : }
4554 : :
4555 [ + + ]: 164 : if (num_extent_pages > 0) {
4556 : 104 : tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t));
4557 [ - + ]: 104 : if (tmp == NULL) {
4558 : 0 : return -ENOMEM;
4559 : : }
4560 : 104 : ctx->extent_page_num = tmp;
4561 : :
4562 : : /* Extent table entries contain md page numbers for extent pages.
4563 : : * Zeroes represent unallocated extent pages, those are run-length-encoded.
4564 : : */
4565 [ + + ]: 208 : for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
4566 [ + - ]: 104 : if (desc_extent_table->extent_page[i].page_idx != 0) {
4567 : 104 : ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx;
4568 : 104 : ctx->num_extent_pages += 1;
4569 : : }
4570 : : }
4571 : : }
4572 : : } else {
4573 : : /* Error */
4574 : 0 : return -EINVAL;
4575 : : }
4576 : : /* Advance to the next descriptor */
4577 : 984 : cur_desc += sizeof(*desc) + desc->length;
4578 [ + + ]: 984 : if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
4579 : 40 : break;
4580 : : }
4581 : 944 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
4582 : : }
4583 : 492 : return 0;
4584 : : }
4585 : :
4586 : : static bool
4587 : 2625 : bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page)
4588 : : {
4589 : : uint32_t crc;
4590 : 2625 : struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors;
4591 : : size_t desc_len;
4592 : :
4593 : 2625 : crc = blob_md_page_calc_crc(page);
4594 [ - + ]: 2625 : if (crc != page->crc) {
4595 : 0 : return false;
4596 : : }
4597 : :
4598 : : /* Extent page should always be of sequence num 0. */
4599 [ + + ]: 2625 : if (page->sequence_num != 0) {
4600 : 88 : return false;
4601 : : }
4602 : :
4603 : : /* Descriptor type must be EXTENT_PAGE. */
4604 [ + + ]: 2537 : if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
4605 : 308 : return false;
4606 : : }
4607 : :
4608 : : /* Descriptor length cannot exceed the page. */
4609 : 2229 : desc_len = sizeof(*desc) + desc->length;
4610 [ - + ]: 2229 : if (desc_len > sizeof(page->descriptors)) {
4611 : 0 : return false;
4612 : : }
4613 : :
4614 : : /* It has to be the only descriptor in the page. */
4615 [ + - ]: 2229 : if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) {
4616 : 2229 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len);
4617 [ - + ]: 2229 : if (desc->length != 0) {
4618 : 0 : return false;
4619 : : }
4620 : : }
4621 : :
4622 : 2229 : return true;
4623 : : }
4624 : :
4625 : : static bool
4626 : 13508 : bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx)
4627 : : {
4628 : : uint32_t crc;
4629 : 13508 : struct spdk_blob_md_page *page = ctx->page;
4630 : :
4631 : 13508 : crc = blob_md_page_calc_crc(page);
4632 [ + + ]: 13508 : if (crc != page->crc) {
4633 : 13076 : return false;
4634 : : }
4635 : :
4636 : : /* First page of a sequence should match the blobid. */
4637 [ + + ]: 432 : if (page->sequence_num == 0 &&
4638 [ + + ]: 344 : bs_page_to_blobid(ctx->cur_page) != page->id) {
4639 : 36 : return false;
4640 : : }
4641 [ - + ]: 396 : assert(bs_load_cur_extent_page_valid(page) == false);
4642 : :
4643 : 396 : return true;
4644 : : }
4645 : :
4646 : : static void bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx);
4647 : :
4648 : : static void
4649 : 212 : bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4650 : : {
4651 : 212 : struct spdk_bs_load_ctx *ctx = cb_arg;
4652 : :
4653 [ - + ]: 212 : if (bserrno != 0) {
4654 : 0 : bs_load_ctx_fail(ctx, bserrno);
4655 : 0 : return;
4656 : : }
4657 : :
4658 : 212 : bs_load_complete(ctx);
4659 : : }
4660 : :
4661 : : static void
4662 : 212 : bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4663 : : {
4664 : 212 : struct spdk_bs_load_ctx *ctx = cb_arg;
4665 : :
4666 : 212 : spdk_free(ctx->mask);
4667 : 212 : ctx->mask = NULL;
4668 : :
4669 [ - + ]: 212 : if (bserrno != 0) {
4670 : 0 : bs_load_ctx_fail(ctx, bserrno);
4671 : 0 : return;
4672 : : }
4673 : :
4674 : 212 : bs_write_used_clusters(seq, ctx, bs_load_write_used_clusters_cpl);
4675 : : }
4676 : :
4677 : : static void
4678 : 212 : bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4679 : : {
4680 : 212 : struct spdk_bs_load_ctx *ctx = cb_arg;
4681 : :
4682 : 212 : spdk_free(ctx->mask);
4683 : 212 : ctx->mask = NULL;
4684 : :
4685 [ - + ]: 212 : if (bserrno != 0) {
4686 : 0 : bs_load_ctx_fail(ctx, bserrno);
4687 : 0 : return;
4688 : : }
4689 : :
4690 : 212 : bs_write_used_blobids(seq, ctx, bs_load_write_used_blobids_cpl);
4691 : : }
4692 : :
4693 : : static void
4694 : 212 : bs_load_write_used_md(struct spdk_bs_load_ctx *ctx)
4695 : : {
4696 : 212 : bs_write_used_md(ctx->seq, ctx, bs_load_write_used_pages_cpl);
4697 : 212 : }
4698 : :
4699 : : static void
4700 : 13428 : bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx)
4701 : : {
4702 : : uint64_t num_md_clusters;
4703 : : uint64_t i;
4704 : :
4705 : 13428 : ctx->in_page_chain = false;
4706 : :
4707 : : do {
4708 : 13568 : ctx->page_index++;
4709 [ + + ]: 13568 : } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true);
4710 : :
4711 [ + + ]: 13428 : if (ctx->page_index < ctx->super->md_len) {
4712 : 13216 : ctx->cur_page = ctx->page_index;
4713 : 13216 : bs_load_replay_cur_md_page(ctx);
4714 : : } else {
4715 : : /* Claim all of the clusters used by the metadata */
4716 : 212 : num_md_clusters = spdk_divide_round_up(
4717 : 212 : ctx->super->md_start + ctx->super->md_len, ctx->bs->pages_per_cluster);
4718 [ + + ]: 960 : for (i = 0; i < num_md_clusters; i++) {
4719 : 748 : spdk_bit_array_set(ctx->used_clusters, i);
4720 : : }
4721 : 212 : ctx->bs->num_free_clusters -= num_md_clusters;
4722 : 212 : spdk_free(ctx->page);
4723 : 212 : bs_load_write_used_md(ctx);
4724 : : }
4725 : 13428 : }
4726 : :
4727 : : static void
4728 : 104 : bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4729 : : {
4730 : 104 : struct spdk_bs_load_ctx *ctx = cb_arg;
4731 : : uint32_t page_num;
4732 : : uint64_t i;
4733 : :
4734 [ - + ]: 104 : if (bserrno != 0) {
4735 : 0 : spdk_free(ctx->extent_pages);
4736 : 0 : bs_load_ctx_fail(ctx, bserrno);
4737 : 0 : return;
4738 : : }
4739 : :
4740 [ + + ]: 208 : for (i = 0; i < ctx->num_extent_pages; i++) {
4741 : : /* Extent pages are only read when present within in chain md.
4742 : : * Integrity of md is not right if that page was not a valid extent page. */
4743 [ - + ]: 104 : if (bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) {
4744 : 0 : spdk_free(ctx->extent_pages);
4745 : 0 : bs_load_ctx_fail(ctx, -EILSEQ);
4746 : 0 : return;
4747 : : }
4748 : :
4749 : 104 : page_num = ctx->extent_page_num[i];
4750 : 104 : spdk_bit_array_set(ctx->bs->used_md_pages, page_num);
4751 [ - + ]: 104 : if (bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) {
4752 : 0 : spdk_free(ctx->extent_pages);
4753 : 0 : bs_load_ctx_fail(ctx, -EILSEQ);
4754 : 0 : return;
4755 : : }
4756 : : }
4757 : :
4758 : 104 : spdk_free(ctx->extent_pages);
4759 : 104 : free(ctx->extent_page_num);
4760 : 104 : ctx->extent_page_num = NULL;
4761 : 104 : ctx->num_extent_pages = 0;
4762 : :
4763 : 104 : bs_load_replay_md_chain_cpl(ctx);
4764 : : }
4765 : :
4766 : : static void
4767 : 104 : bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx)
4768 : : {
4769 : : spdk_bs_batch_t *batch;
4770 : : uint32_t page;
4771 : : uint64_t lba;
4772 : : uint64_t i;
4773 : :
4774 : 104 : ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, 0,
4775 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4776 [ - + ]: 104 : if (!ctx->extent_pages) {
4777 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4778 : 0 : return;
4779 : : }
4780 : :
4781 : 104 : batch = bs_sequence_to_batch(ctx->seq, bs_load_replay_extent_page_cpl, ctx);
4782 : :
4783 [ + + ]: 208 : for (i = 0; i < ctx->num_extent_pages; i++) {
4784 : 104 : page = ctx->extent_page_num[i];
4785 [ - + ]: 104 : assert(page < ctx->super->md_len);
4786 : 104 : lba = bs_md_page_to_lba(ctx->bs, page);
4787 : 104 : bs_batch_read_dev(batch, &ctx->extent_pages[i], lba,
4788 : 104 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE));
4789 : : }
4790 : :
4791 : 104 : bs_batch_close(batch);
4792 : : }
4793 : :
4794 : : static void
4795 : 13508 : bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4796 : : {
4797 : 13508 : struct spdk_bs_load_ctx *ctx = cb_arg;
4798 : : uint32_t page_num;
4799 : : struct spdk_blob_md_page *page;
4800 : :
4801 [ - + ]: 13508 : if (bserrno != 0) {
4802 : 0 : bs_load_ctx_fail(ctx, bserrno);
4803 : 0 : return;
4804 : : }
4805 : :
4806 : 13508 : page_num = ctx->cur_page;
4807 : 13508 : page = ctx->page;
4808 [ + + ]: 13508 : if (bs_load_cur_md_page_valid(ctx) == true) {
4809 [ + + + + ]: 396 : if (page->sequence_num == 0 || ctx->in_page_chain == true) {
4810 : 388 : spdk_spin_lock(&ctx->bs->used_lock);
4811 : 388 : bs_claim_md_page(ctx->bs, page_num);
4812 : 388 : spdk_spin_unlock(&ctx->bs->used_lock);
4813 [ + + ]: 388 : if (page->sequence_num == 0) {
4814 : 308 : SPDK_NOTICELOG("Recover: blob 0x%" PRIx32 "\n", page_num);
4815 : 308 : spdk_bit_array_set(ctx->bs->used_blobids, page_num);
4816 : : }
4817 [ - + ]: 388 : if (bs_load_replay_md_parse_page(ctx, page)) {
4818 : 0 : bs_load_ctx_fail(ctx, -EILSEQ);
4819 : 0 : return;
4820 : : }
4821 [ + + ]: 388 : if (page->next != SPDK_INVALID_MD_PAGE) {
4822 : 80 : ctx->in_page_chain = true;
4823 : 80 : ctx->cur_page = page->next;
4824 : 80 : bs_load_replay_cur_md_page(ctx);
4825 : 80 : return;
4826 : : }
4827 [ + + ]: 308 : if (ctx->num_extent_pages != 0) {
4828 : 104 : bs_load_replay_extent_pages(ctx);
4829 : 104 : return;
4830 : : }
4831 : : }
4832 : : }
4833 : 13324 : bs_load_replay_md_chain_cpl(ctx);
4834 : : }
4835 : :
4836 : : static void
4837 : 13508 : bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx)
4838 : : {
4839 : : uint64_t lba;
4840 : :
4841 [ - + ]: 13508 : assert(ctx->cur_page < ctx->super->md_len);
4842 : 13508 : lba = bs_md_page_to_lba(ctx->bs, ctx->cur_page);
4843 : 13508 : bs_sequence_read_dev(ctx->seq, ctx->page, lba,
4844 : 13508 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
4845 : : bs_load_replay_md_cpl, ctx);
4846 : 13508 : }
4847 : :
4848 : : static void
4849 : 212 : bs_load_replay_md(struct spdk_bs_load_ctx *ctx)
4850 : : {
4851 : 212 : ctx->page_index = 0;
4852 : 212 : ctx->cur_page = 0;
4853 : 212 : ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
4854 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4855 [ - + ]: 212 : if (!ctx->page) {
4856 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4857 : 0 : return;
4858 : : }
4859 : 212 : bs_load_replay_cur_md_page(ctx);
4860 : : }
4861 : :
4862 : : static void
4863 : 212 : bs_recover(struct spdk_bs_load_ctx *ctx)
4864 : : {
4865 : : int rc;
4866 : :
4867 : 212 : SPDK_NOTICELOG("Performing recovery on blobstore\n");
4868 : 212 : rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len);
4869 [ - + ]: 212 : if (rc < 0) {
4870 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4871 : 0 : return;
4872 : : }
4873 : :
4874 : 212 : rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len);
4875 [ - + ]: 212 : if (rc < 0) {
4876 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4877 : 0 : return;
4878 : : }
4879 : :
4880 : 212 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
4881 [ - + ]: 212 : if (rc < 0) {
4882 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4883 : 0 : return;
4884 : : }
4885 : :
4886 : 212 : rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->super->md_len);
4887 [ - + ]: 212 : if (rc < 0) {
4888 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
4889 : 0 : return;
4890 : : }
4891 : :
4892 : 212 : ctx->bs->num_free_clusters = ctx->bs->total_clusters;
4893 : 212 : bs_load_replay_md(ctx);
4894 : : }
4895 : :
4896 : : static int
4897 : 558 : bs_parse_super(struct spdk_bs_load_ctx *ctx)
4898 : : {
4899 : : int rc;
4900 : :
4901 [ + + ]: 558 : if (ctx->super->size == 0) {
4902 : 16 : ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
4903 : : }
4904 : :
4905 [ + + ]: 558 : if (ctx->super->io_unit_size == 0) {
4906 : 16 : ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
4907 : : }
4908 : :
4909 : 558 : ctx->bs->clean = 1;
4910 : 558 : ctx->bs->cluster_sz = ctx->super->cluster_size;
4911 : 558 : ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
4912 : 558 : ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
4913 [ + - ]: 558 : if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
4914 : 558 : ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
4915 : : }
4916 : 558 : ctx->bs->io_unit_size = ctx->super->io_unit_size;
4917 : 558 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
4918 [ - + ]: 558 : if (rc < 0) {
4919 : 0 : return -ENOMEM;
4920 : : }
4921 : 558 : ctx->bs->md_start = ctx->super->md_start;
4922 : 558 : ctx->bs->md_len = ctx->super->md_len;
4923 : 558 : rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
4924 [ - + ]: 558 : if (rc < 0) {
4925 : 0 : return -ENOMEM;
4926 : : }
4927 : :
4928 : 1116 : ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
4929 : 558 : ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
4930 : 558 : ctx->bs->super_blob = ctx->super->super_blob;
4931 : 558 : memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
4932 : :
4933 : 558 : return 0;
4934 : : }
4935 : :
4936 : : static void
4937 : 2344 : bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4938 : : {
4939 : 2344 : struct spdk_bs_load_ctx *ctx = cb_arg;
4940 : : int rc;
4941 : :
4942 : 2344 : rc = bs_super_validate(ctx->super, ctx->bs);
4943 [ + + ]: 2344 : if (rc != 0) {
4944 : 1786 : bs_load_ctx_fail(ctx, rc);
4945 : 1786 : return;
4946 : : }
4947 : :
4948 : 558 : rc = bs_parse_super(ctx);
4949 [ - + ]: 558 : if (rc < 0) {
4950 : 0 : bs_load_ctx_fail(ctx, rc);
4951 : 0 : return;
4952 : : }
4953 : :
4954 [ + + + + : 558 : if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0 || ctx->force_recover) {
- + ]
4955 : 212 : bs_recover(ctx);
4956 : : } else {
4957 : 346 : bs_load_read_used_pages(ctx);
4958 : : }
4959 : : }
4960 : :
4961 : : static inline int
4962 : 2377 : bs_opts_copy(struct spdk_bs_opts *src, struct spdk_bs_opts *dst)
4963 : : {
4964 : :
4965 [ - + ]: 2377 : if (!src->opts_size) {
4966 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
4967 : 0 : return -1;
4968 : : }
4969 : :
4970 : : #define FIELD_OK(field) \
4971 : : offsetof(struct spdk_bs_opts, field) + sizeof(src->field) <= src->opts_size
4972 : :
4973 : : #define SET_FIELD(field) \
4974 : : if (FIELD_OK(field)) { \
4975 : : dst->field = src->field; \
4976 : : } \
4977 : :
4978 [ + - ]: 2377 : SET_FIELD(cluster_sz);
4979 [ + - ]: 2377 : SET_FIELD(num_md_pages);
4980 [ + - ]: 2377 : SET_FIELD(max_md_ops);
4981 [ + - ]: 2377 : SET_FIELD(max_channel_ops);
4982 [ + - ]: 2377 : SET_FIELD(clear_method);
4983 : :
4984 [ + - ]: 2377 : if (FIELD_OK(bstype)) {
4985 : 2377 : memcpy(&dst->bstype, &src->bstype, sizeof(dst->bstype));
4986 : : }
4987 [ + - ]: 2377 : SET_FIELD(iter_cb_fn);
4988 [ + - ]: 2377 : SET_FIELD(iter_cb_arg);
4989 [ + - ]: 2377 : SET_FIELD(force_recover);
4990 [ + - ]: 2377 : SET_FIELD(esnap_bs_dev_create);
4991 [ + - ]: 2377 : SET_FIELD(esnap_ctx);
4992 : :
4993 : 2377 : dst->opts_size = src->opts_size;
4994 : :
4995 : : /* You should not remove this statement, but need to update the assert statement
4996 : : * if you add a new field, and also add a corresponding SET_FIELD statement */
4997 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_opts) == 88, "Incorrect size");
4998 : :
4999 : : #undef FIELD_OK
5000 : : #undef SET_FIELD
5001 : :
5002 : 2377 : return 0;
5003 : : }
5004 : :
5005 : : void
5006 : 2376 : spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
5007 : : spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
5008 : : {
5009 : 2250 : struct spdk_blob_store *bs;
5010 : 2250 : struct spdk_bs_cpl cpl;
5011 : 2250 : struct spdk_bs_load_ctx *ctx;
5012 : 2376 : struct spdk_bs_opts opts = {};
5013 : : int err;
5014 : :
5015 [ - + ]: 2376 : SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
5016 : :
5017 [ + + ]: 2376 : if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
5018 [ - + ]: 8 : SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
5019 : 8 : dev->destroy(dev);
5020 : 8 : cb_fn(cb_arg, NULL, -EINVAL);
5021 : 12 : return;
5022 : : }
5023 : :
5024 : 2368 : spdk_bs_opts_init(&opts, sizeof(opts));
5025 [ + + ]: 2368 : if (o) {
5026 [ - + ]: 1996 : if (bs_opts_copy(o, &opts)) {
5027 : 0 : return;
5028 : : }
5029 : : }
5030 : :
5031 [ + + + + ]: 2368 : if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
5032 : 16 : dev->destroy(dev);
5033 : 16 : cb_fn(cb_arg, NULL, -EINVAL);
5034 : 16 : return;
5035 : : }
5036 : :
5037 : 2352 : err = bs_alloc(dev, &opts, &bs, &ctx);
5038 [ + + ]: 2352 : if (err) {
5039 : 8 : dev->destroy(dev);
5040 : 8 : cb_fn(cb_arg, NULL, err);
5041 : 8 : return;
5042 : : }
5043 : :
5044 : 2344 : cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
5045 : 2344 : cpl.u.bs_handle.cb_fn = cb_fn;
5046 : 2344 : cpl.u.bs_handle.cb_arg = cb_arg;
5047 : 2344 : cpl.u.bs_handle.bs = bs;
5048 : :
5049 : 2344 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5050 [ - + ]: 2344 : if (!ctx->seq) {
5051 : 0 : spdk_free(ctx->super);
5052 : 0 : free(ctx);
5053 : 0 : bs_free(bs);
5054 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5055 : 0 : return;
5056 : : }
5057 : :
5058 : : /* Read the super block */
5059 : 2344 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
5060 : 2344 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
5061 : : bs_load_super_cpl, ctx);
5062 : : }
5063 : :
5064 : : /* END spdk_bs_load */
5065 : :
5066 : : /* START spdk_bs_dump */
5067 : :
5068 : : static void
5069 : 0 : bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno)
5070 : : {
5071 : 0 : spdk_free(ctx->super);
5072 : :
5073 : : /*
5074 : : * We need to defer calling bs_call_cpl() until after
5075 : : * dev destruction, so tuck these away for later use.
5076 : : */
5077 : 0 : ctx->bs->unload_err = bserrno;
5078 : 0 : memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
5079 : 0 : seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
5080 : :
5081 : 0 : bs_sequence_finish(seq, 0);
5082 : 0 : bs_free(ctx->bs);
5083 : 0 : free(ctx);
5084 : 0 : }
5085 : :
5086 : : static void
5087 : 0 : bs_dump_print_xattr(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
5088 : : {
5089 : : struct spdk_blob_md_descriptor_xattr *desc_xattr;
5090 : : uint32_t i;
5091 : : const char *type;
5092 : :
5093 : 0 : desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc;
5094 : :
5095 : 0 : if (desc_xattr->length !=
5096 : : sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) +
5097 : 0 : desc_xattr->name_length + desc_xattr->value_length) {
5098 : : }
5099 : :
5100 : 0 : memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length);
5101 : 0 : ctx->xattr_name[desc_xattr->name_length] = '\0';
5102 [ # # ]: 0 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
5103 : 0 : type = "XATTR";
5104 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
5105 : 0 : type = "XATTR_INTERNAL";
5106 : : } else {
5107 : 0 : assert(false);
5108 : : type = "XATTR_?";
5109 : : }
5110 : 0 : fprintf(ctx->fp, "%s: name = \"%s\"\n", type, ctx->xattr_name);
5111 : 0 : fprintf(ctx->fp, " value = \"");
5112 : 0 : ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name,
5113 : 0 : (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
5114 : 0 : desc_xattr->value_length);
5115 : 0 : fprintf(ctx->fp, "\"\n");
5116 [ # # ]: 0 : for (i = 0; i < desc_xattr->value_length; i++) {
5117 [ # # ]: 0 : if (i % 16 == 0) {
5118 : 0 : fprintf(ctx->fp, " ");
5119 : : }
5120 : 0 : fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i));
5121 [ # # ]: 0 : if ((i + 1) % 16 == 0) {
5122 : 0 : fprintf(ctx->fp, "\n");
5123 : : }
5124 : : }
5125 [ # # ]: 0 : if (i % 16 != 0) {
5126 : 0 : fprintf(ctx->fp, "\n");
5127 : : }
5128 : 0 : }
5129 : :
5130 : : struct type_flag_desc {
5131 : : uint64_t mask;
5132 : : uint64_t val;
5133 : : const char *name;
5134 : : };
5135 : :
5136 : : static void
5137 : 0 : bs_dump_print_type_bits(struct spdk_bs_load_ctx *ctx, uint64_t flags,
5138 : : struct type_flag_desc *desc, size_t numflags)
5139 : : {
5140 : 0 : uint64_t covered = 0;
5141 : : size_t i;
5142 : :
5143 [ # # ]: 0 : for (i = 0; i < numflags; i++) {
5144 [ # # ]: 0 : if ((desc[i].mask & flags) != desc[i].val) {
5145 : 0 : continue;
5146 : : }
5147 : 0 : fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " %s", desc[i].val, desc[i].name);
5148 [ # # ]: 0 : if (desc[i].mask != desc[i].val) {
5149 : 0 : fprintf(ctx->fp, " (mask 0x%" PRIx64 " value 0x%" PRIx64 ")",
5150 : 0 : desc[i].mask, desc[i].val);
5151 : : }
5152 : 0 : fprintf(ctx->fp, "\n");
5153 : 0 : covered |= desc[i].mask;
5154 : : }
5155 [ # # ]: 0 : if ((flags & ~covered) != 0) {
5156 : 0 : fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " Unknown\n", flags & ~covered);
5157 : : }
5158 : 0 : }
5159 : :
5160 : : static void
5161 : 0 : bs_dump_print_type_flags(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
5162 : : {
5163 : : struct spdk_blob_md_descriptor_flags *type_desc;
5164 : : #define ADD_FLAG(f) { f, f, #f }
5165 : : #define ADD_MASK_VAL(m, v) { m, v, #v }
5166 : : static struct type_flag_desc invalid[] = {
5167 : : ADD_FLAG(SPDK_BLOB_THIN_PROV),
5168 : : ADD_FLAG(SPDK_BLOB_INTERNAL_XATTR),
5169 : : ADD_FLAG(SPDK_BLOB_EXTENT_TABLE),
5170 : : };
5171 : : static struct type_flag_desc data_ro[] = {
5172 : : ADD_FLAG(SPDK_BLOB_READ_ONLY),
5173 : : };
5174 : : static struct type_flag_desc md_ro[] = {
5175 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_DEFAULT),
5176 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_NONE),
5177 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_UNMAP),
5178 : : ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_WRITE_ZEROES),
5179 : : };
5180 : : #undef ADD_FLAG
5181 : : #undef ADD_MASK_VAL
5182 : :
5183 : 0 : type_desc = (struct spdk_blob_md_descriptor_flags *)desc;
5184 : 0 : fprintf(ctx->fp, "Flags:\n");
5185 : 0 : fprintf(ctx->fp, "\tinvalid: 0x%016" PRIx64 "\n", type_desc->invalid_flags);
5186 : 0 : bs_dump_print_type_bits(ctx, type_desc->invalid_flags, invalid,
5187 : : SPDK_COUNTOF(invalid));
5188 : 0 : fprintf(ctx->fp, "\tdata_ro: 0x%016" PRIx64 "\n", type_desc->data_ro_flags);
5189 : 0 : bs_dump_print_type_bits(ctx, type_desc->data_ro_flags, data_ro,
5190 : : SPDK_COUNTOF(data_ro));
5191 : 0 : fprintf(ctx->fp, "\t md_ro: 0x%016" PRIx64 "\n", type_desc->md_ro_flags);
5192 : 0 : bs_dump_print_type_bits(ctx, type_desc->md_ro_flags, md_ro,
5193 : : SPDK_COUNTOF(md_ro));
5194 : 0 : }
5195 : :
5196 : : static void
5197 : 0 : bs_dump_print_extent_table(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
5198 : : {
5199 : : struct spdk_blob_md_descriptor_extent_table *et_desc;
5200 : : uint64_t num_extent_pages;
5201 : : uint32_t et_idx;
5202 : :
5203 : 0 : et_desc = (struct spdk_blob_md_descriptor_extent_table *)desc;
5204 : 0 : num_extent_pages = (et_desc->length - sizeof(et_desc->num_clusters)) /
5205 : : sizeof(et_desc->extent_page[0]);
5206 : :
5207 : 0 : fprintf(ctx->fp, "Extent table:\n");
5208 [ # # ]: 0 : for (et_idx = 0; et_idx < num_extent_pages; et_idx++) {
5209 [ # # ]: 0 : if (et_desc->extent_page[et_idx].page_idx == 0) {
5210 : : /* Zeroes represent unallocated extent pages. */
5211 : 0 : continue;
5212 : : }
5213 : 0 : fprintf(ctx->fp, "\tExtent page: %5" PRIu32 " length %3" PRIu32
5214 : : " at LBA %" PRIu64 "\n", et_desc->extent_page[et_idx].page_idx,
5215 : : et_desc->extent_page[et_idx].num_pages,
5216 : : bs_md_page_to_lba(ctx->bs, et_desc->extent_page[et_idx].page_idx));
5217 : : }
5218 : 0 : }
5219 : :
5220 : : static void
5221 : 0 : bs_dump_print_md_page(struct spdk_bs_load_ctx *ctx)
5222 : : {
5223 : 0 : uint32_t page_idx = ctx->cur_page;
5224 : 0 : struct spdk_blob_md_page *page = ctx->page;
5225 : : struct spdk_blob_md_descriptor *desc;
5226 : 0 : size_t cur_desc = 0;
5227 : : uint32_t crc;
5228 : :
5229 : 0 : fprintf(ctx->fp, "=========\n");
5230 : 0 : fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx);
5231 : 0 : fprintf(ctx->fp, "Start LBA: %" PRIu64 "\n", bs_md_page_to_lba(ctx->bs, page_idx));
5232 : 0 : fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id);
5233 : 0 : fprintf(ctx->fp, "Sequence: %" PRIu32 "\n", page->sequence_num);
5234 [ # # ]: 0 : if (page->next == SPDK_INVALID_MD_PAGE) {
5235 : 0 : fprintf(ctx->fp, "Next: None\n");
5236 : : } else {
5237 : 0 : fprintf(ctx->fp, "Next: %" PRIu32 "\n", page->next);
5238 : : }
5239 [ # # ]: 0 : fprintf(ctx->fp, "In used bit array%s:", ctx->super->clean ? "" : " (not clean: dubious)");
5240 [ # # ]: 0 : if (spdk_bit_array_get(ctx->bs->used_md_pages, page_idx)) {
5241 : 0 : fprintf(ctx->fp, " md");
5242 : : }
5243 [ # # ]: 0 : if (spdk_bit_array_get(ctx->bs->used_blobids, page_idx)) {
5244 : 0 : fprintf(ctx->fp, " blob");
5245 : : }
5246 : 0 : fprintf(ctx->fp, "\n");
5247 : :
5248 : 0 : crc = blob_md_page_calc_crc(page);
5249 [ # # ]: 0 : fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch");
5250 : :
5251 : 0 : desc = (struct spdk_blob_md_descriptor *)page->descriptors;
5252 [ # # ]: 0 : while (cur_desc < sizeof(page->descriptors)) {
5253 [ # # ]: 0 : if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
5254 [ # # ]: 0 : if (desc->length == 0) {
5255 : : /* If padding and length are 0, this terminates the page */
5256 : 0 : break;
5257 : : }
5258 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
5259 : : struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
5260 : : unsigned int i;
5261 : :
5262 : 0 : desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
5263 : :
5264 [ # # ]: 0 : for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
5265 [ # # ]: 0 : if (desc_extent_rle->extents[i].cluster_idx != 0) {
5266 : 0 : fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
5267 : : desc_extent_rle->extents[i].cluster_idx);
5268 : : } else {
5269 : 0 : fprintf(ctx->fp, "Unallocated Extent - ");
5270 : : }
5271 : 0 : fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length);
5272 : 0 : fprintf(ctx->fp, "\n");
5273 : : }
5274 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
5275 : : struct spdk_blob_md_descriptor_extent_page *desc_extent;
5276 : : unsigned int i;
5277 : :
5278 : 0 : desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
5279 : :
5280 [ # # ]: 0 : for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) {
5281 [ # # ]: 0 : if (desc_extent->cluster_idx[i] != 0) {
5282 : 0 : fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
5283 : : desc_extent->cluster_idx[i]);
5284 : : } else {
5285 : 0 : fprintf(ctx->fp, "Unallocated Extent");
5286 : : }
5287 : 0 : fprintf(ctx->fp, "\n");
5288 : : }
5289 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
5290 : 0 : bs_dump_print_xattr(ctx, desc);
5291 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
5292 : 0 : bs_dump_print_xattr(ctx, desc);
5293 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
5294 : 0 : bs_dump_print_type_flags(ctx, desc);
5295 [ # # ]: 0 : } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
5296 : 0 : bs_dump_print_extent_table(ctx, desc);
5297 : : } else {
5298 : : /* Error */
5299 : 0 : fprintf(ctx->fp, "Unknown descriptor type %" PRIu8 "\n", desc->type);
5300 : : }
5301 : : /* Advance to the next descriptor */
5302 : 0 : cur_desc += sizeof(*desc) + desc->length;
5303 [ # # ]: 0 : if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
5304 : 0 : break;
5305 : : }
5306 : 0 : desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
5307 : : }
5308 : 0 : }
5309 : :
5310 : : static void
5311 : 0 : bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5312 : : {
5313 : 0 : struct spdk_bs_load_ctx *ctx = cb_arg;
5314 : :
5315 [ # # ]: 0 : if (bserrno != 0) {
5316 : 0 : bs_dump_finish(seq, ctx, bserrno);
5317 : 0 : return;
5318 : : }
5319 : :
5320 [ # # ]: 0 : if (ctx->page->id != 0) {
5321 : 0 : bs_dump_print_md_page(ctx);
5322 : : }
5323 : :
5324 : 0 : ctx->cur_page++;
5325 : :
5326 [ # # ]: 0 : if (ctx->cur_page < ctx->super->md_len) {
5327 : 0 : bs_dump_read_md_page(seq, ctx);
5328 : : } else {
5329 : 0 : spdk_free(ctx->page);
5330 : 0 : bs_dump_finish(seq, ctx, 0);
5331 : : }
5332 : : }
5333 : :
5334 : : static void
5335 : 0 : bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg)
5336 : : {
5337 : 0 : struct spdk_bs_load_ctx *ctx = cb_arg;
5338 : : uint64_t lba;
5339 : :
5340 [ # # ]: 0 : assert(ctx->cur_page < ctx->super->md_len);
5341 : 0 : lba = bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page);
5342 : 0 : bs_sequence_read_dev(seq, ctx->page, lba,
5343 : 0 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
5344 : : bs_dump_read_md_page_cpl, ctx);
5345 : 0 : }
5346 : :
5347 : : static void
5348 : 0 : bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5349 : : {
5350 : 0 : struct spdk_bs_load_ctx *ctx = cb_arg;
5351 : : int rc;
5352 : :
5353 : 0 : fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature);
5354 [ # # ]: 0 : if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
5355 : : sizeof(ctx->super->signature)) != 0) {
5356 : 0 : fprintf(ctx->fp, "(Mismatch)\n");
5357 : 0 : bs_dump_finish(seq, ctx, bserrno);
5358 : 0 : return;
5359 : : } else {
5360 : 0 : fprintf(ctx->fp, "(OK)\n");
5361 : : }
5362 : 0 : fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version);
5363 [ # # ]: 0 : fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc,
5364 : 0 : (ctx->super->crc == blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch");
5365 : 0 : fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype);
5366 : 0 : fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size);
5367 : 0 : fprintf(ctx->fp, "Super Blob ID: ");
5368 [ # # ]: 0 : if (ctx->super->super_blob == SPDK_BLOBID_INVALID) {
5369 : 0 : fprintf(ctx->fp, "(None)\n");
5370 : : } else {
5371 : 0 : fprintf(ctx->fp, "0x%" PRIx64 "\n", ctx->super->super_blob);
5372 : : }
5373 : 0 : fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean);
5374 : 0 : fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start);
5375 : 0 : fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len);
5376 : 0 : fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start);
5377 : 0 : fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len);
5378 : 0 : fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start);
5379 : 0 : fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len);
5380 : 0 : fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start);
5381 : 0 : fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len);
5382 : :
5383 : 0 : ctx->cur_page = 0;
5384 : 0 : ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
5385 : : NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
5386 [ # # ]: 0 : if (!ctx->page) {
5387 : 0 : bs_dump_finish(seq, ctx, -ENOMEM);
5388 : 0 : return;
5389 : : }
5390 : :
5391 : 0 : rc = bs_parse_super(ctx);
5392 [ # # ]: 0 : if (rc < 0) {
5393 : 0 : bs_load_ctx_fail(ctx, rc);
5394 : 0 : return;
5395 : : }
5396 : :
5397 : 0 : bs_load_read_used_pages(ctx);
5398 : : }
5399 : :
5400 : : void
5401 : 0 : spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn,
5402 : : spdk_bs_op_complete cb_fn, void *cb_arg)
5403 : : {
5404 : 0 : struct spdk_blob_store *bs;
5405 : 0 : struct spdk_bs_cpl cpl;
5406 : 0 : struct spdk_bs_load_ctx *ctx;
5407 : 0 : struct spdk_bs_opts opts = {};
5408 : : int err;
5409 : :
5410 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "Dumping blobstore from dev %p\n", dev);
5411 : :
5412 : 0 : spdk_bs_opts_init(&opts, sizeof(opts));
5413 : :
5414 : 0 : err = bs_alloc(dev, &opts, &bs, &ctx);
5415 [ # # ]: 0 : if (err) {
5416 : 0 : dev->destroy(dev);
5417 : 0 : cb_fn(cb_arg, err);
5418 : 0 : return;
5419 : : }
5420 : :
5421 : 0 : ctx->dumping = true;
5422 : 0 : ctx->fp = fp;
5423 : 0 : ctx->print_xattr_fn = print_xattr_fn;
5424 : :
5425 : 0 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5426 : 0 : cpl.u.bs_basic.cb_fn = cb_fn;
5427 : 0 : cpl.u.bs_basic.cb_arg = cb_arg;
5428 : :
5429 : 0 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5430 [ # # ]: 0 : if (!ctx->seq) {
5431 : 0 : spdk_free(ctx->super);
5432 : 0 : free(ctx);
5433 : 0 : bs_free(bs);
5434 : 0 : cb_fn(cb_arg, -ENOMEM);
5435 : 0 : return;
5436 : : }
5437 : :
5438 : : /* Read the super block */
5439 : 0 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
5440 : 0 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
5441 : : bs_dump_super_cpl, ctx);
5442 : : }
5443 : :
5444 : : /* END spdk_bs_dump */
5445 : :
5446 : : /* START spdk_bs_init */
5447 : :
5448 : : static void
5449 : 953 : bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5450 : : {
5451 : 953 : struct spdk_bs_load_ctx *ctx = cb_arg;
5452 : :
5453 : 953 : ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
5454 : 953 : spdk_free(ctx->super);
5455 : 953 : free(ctx);
5456 : :
5457 : 953 : bs_sequence_finish(seq, bserrno);
5458 : 953 : }
5459 : :
5460 : : static void
5461 : 953 : bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5462 : : {
5463 : 953 : struct spdk_bs_load_ctx *ctx = cb_arg;
5464 : :
5465 : : /* Write super block */
5466 : 953 : bs_sequence_write_dev(seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
5467 : 953 : bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
5468 : : bs_init_persist_super_cpl, ctx);
5469 : 953 : }
5470 : :
5471 : : void
5472 : 985 : spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
5473 : : spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
5474 : : {
5475 : 981 : struct spdk_bs_load_ctx *ctx;
5476 : 981 : struct spdk_blob_store *bs;
5477 : 981 : struct spdk_bs_cpl cpl;
5478 : : spdk_bs_sequence_t *seq;
5479 : : spdk_bs_batch_t *batch;
5480 : : uint64_t num_md_lba;
5481 : : uint64_t num_md_pages;
5482 : : uint64_t num_md_clusters;
5483 : : uint64_t max_used_cluster_mask_len;
5484 : : uint32_t i;
5485 : 985 : struct spdk_bs_opts opts = {};
5486 : : int rc;
5487 : : uint64_t lba, lba_count;
5488 : :
5489 [ - + ]: 985 : SPDK_DEBUGLOG(blob, "Initializing blobstore on dev %p\n", dev);
5490 : :
5491 [ + + ]: 985 : if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
5492 : 8 : SPDK_ERRLOG("unsupported dev block length of %d\n",
5493 : : dev->blocklen);
5494 : 8 : dev->destroy(dev);
5495 : 8 : cb_fn(cb_arg, NULL, -EINVAL);
5496 : 8 : return;
5497 : : }
5498 : :
5499 : 977 : spdk_bs_opts_init(&opts, sizeof(opts));
5500 [ + + ]: 977 : if (o) {
5501 [ - + ]: 373 : if (bs_opts_copy(o, &opts)) {
5502 : 0 : return;
5503 : : }
5504 : : }
5505 : :
5506 [ + + ]: 977 : if (bs_opts_verify(&opts) != 0) {
5507 : 8 : dev->destroy(dev);
5508 : 8 : cb_fn(cb_arg, NULL, -EINVAL);
5509 : 8 : return;
5510 : : }
5511 : :
5512 : 969 : rc = bs_alloc(dev, &opts, &bs, &ctx);
5513 [ + + ]: 969 : if (rc) {
5514 : 8 : dev->destroy(dev);
5515 : 8 : cb_fn(cb_arg, NULL, rc);
5516 : 8 : return;
5517 : : }
5518 : :
5519 [ + + ]: 961 : if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
5520 : : /* By default, allocate 1 page per cluster.
5521 : : * Technically, this over-allocates metadata
5522 : : * because more metadata will reduce the number
5523 : : * of usable clusters. This can be addressed with
5524 : : * more complex math in the future.
5525 : : */
5526 : 936 : bs->md_len = bs->total_clusters;
5527 : : } else {
5528 : 25 : bs->md_len = opts.num_md_pages;
5529 : : }
5530 : 961 : rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
5531 [ - + ]: 961 : if (rc < 0) {
5532 : 0 : spdk_free(ctx->super);
5533 : 0 : free(ctx);
5534 : 0 : bs_free(bs);
5535 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5536 : 0 : return;
5537 : : }
5538 : :
5539 : 961 : rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len);
5540 [ - + ]: 961 : if (rc < 0) {
5541 : 0 : spdk_free(ctx->super);
5542 : 0 : free(ctx);
5543 : 0 : bs_free(bs);
5544 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5545 : 0 : return;
5546 : : }
5547 : :
5548 : 961 : rc = spdk_bit_array_resize(&bs->open_blobids, bs->md_len);
5549 [ - + ]: 961 : if (rc < 0) {
5550 : 0 : spdk_free(ctx->super);
5551 : 0 : free(ctx);
5552 : 0 : bs_free(bs);
5553 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5554 : 0 : return;
5555 : : }
5556 : :
5557 : 961 : memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
5558 : : sizeof(ctx->super->signature));
5559 : 961 : ctx->super->version = SPDK_BS_VERSION;
5560 : 961 : ctx->super->length = sizeof(*ctx->super);
5561 : 961 : ctx->super->super_blob = bs->super_blob;
5562 : 961 : ctx->super->clean = 0;
5563 : 961 : ctx->super->cluster_size = bs->cluster_sz;
5564 : 961 : ctx->super->io_unit_size = bs->io_unit_size;
5565 : 961 : memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
5566 : :
5567 : : /* Calculate how many pages the metadata consumes at the front
5568 : : * of the disk.
5569 : : */
5570 : :
5571 : : /* The super block uses 1 page */
5572 : 961 : num_md_pages = 1;
5573 : :
5574 : : /* The used_md_pages mask requires 1 bit per metadata page, rounded
5575 : : * up to the nearest page, plus a header.
5576 : : */
5577 : 961 : ctx->super->used_page_mask_start = num_md_pages;
5578 : 961 : ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5579 : 961 : spdk_divide_round_up(bs->md_len, 8),
5580 : : SPDK_BS_PAGE_SIZE);
5581 : 961 : num_md_pages += ctx->super->used_page_mask_len;
5582 : :
5583 : : /* The used_clusters mask requires 1 bit per cluster, rounded
5584 : : * up to the nearest page, plus a header.
5585 : : */
5586 : 961 : ctx->super->used_cluster_mask_start = num_md_pages;
5587 : 961 : ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5588 : 961 : spdk_divide_round_up(bs->total_clusters, 8),
5589 : : SPDK_BS_PAGE_SIZE);
5590 : : /* The blobstore might be extended, then the used_cluster bitmap will need more space.
5591 : : * Here we calculate the max clusters we can support according to the
5592 : : * num_md_pages (bs->md_len).
5593 : : */
5594 : 961 : max_used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5595 : 961 : spdk_divide_round_up(bs->md_len, 8),
5596 : : SPDK_BS_PAGE_SIZE);
5597 : 961 : max_used_cluster_mask_len = spdk_max(max_used_cluster_mask_len,
5598 : : ctx->super->used_cluster_mask_len);
5599 : 961 : num_md_pages += max_used_cluster_mask_len;
5600 : :
5601 : : /* The used_blobids mask requires 1 bit per metadata page, rounded
5602 : : * up to the nearest page, plus a header.
5603 : : */
5604 : 961 : ctx->super->used_blobid_mask_start = num_md_pages;
5605 : 961 : ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
5606 : 961 : spdk_divide_round_up(bs->md_len, 8),
5607 : : SPDK_BS_PAGE_SIZE);
5608 : 961 : num_md_pages += ctx->super->used_blobid_mask_len;
5609 : :
5610 : : /* The metadata region size was chosen above */
5611 : 961 : ctx->super->md_start = bs->md_start = num_md_pages;
5612 : 961 : ctx->super->md_len = bs->md_len;
5613 : 961 : num_md_pages += bs->md_len;
5614 : :
5615 : 961 : num_md_lba = bs_page_to_lba(bs, num_md_pages);
5616 : :
5617 : 961 : ctx->super->size = dev->blockcnt * dev->blocklen;
5618 : :
5619 : 961 : ctx->super->crc = blob_md_page_calc_crc(ctx->super);
5620 : :
5621 : 961 : num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster);
5622 [ + + ]: 961 : if (num_md_clusters > bs->total_clusters) {
5623 : 8 : SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
5624 : : "please decrease number of pages reserved for metadata "
5625 : : "or increase cluster size.\n");
5626 : 8 : spdk_free(ctx->super);
5627 : 8 : spdk_bit_array_free(&ctx->used_clusters);
5628 : 8 : free(ctx);
5629 : 8 : bs_free(bs);
5630 : 8 : cb_fn(cb_arg, NULL, -ENOMEM);
5631 : 8 : return;
5632 : : }
5633 : : /* Claim all of the clusters used by the metadata */
5634 [ + + ]: 151438 : for (i = 0; i < num_md_clusters; i++) {
5635 : 150485 : spdk_bit_array_set(ctx->used_clusters, i);
5636 : : }
5637 : :
5638 : 953 : bs->num_free_clusters -= num_md_clusters;
5639 : 953 : bs->total_data_clusters = bs->num_free_clusters;
5640 : :
5641 : 953 : cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
5642 : 953 : cpl.u.bs_handle.cb_fn = cb_fn;
5643 : 953 : cpl.u.bs_handle.cb_arg = cb_arg;
5644 : 953 : cpl.u.bs_handle.bs = bs;
5645 : :
5646 : 953 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5647 [ - + ]: 953 : if (!seq) {
5648 : 0 : spdk_free(ctx->super);
5649 : 0 : free(ctx);
5650 : 0 : bs_free(bs);
5651 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
5652 : 0 : return;
5653 : : }
5654 : :
5655 : 953 : batch = bs_sequence_to_batch(seq, bs_init_trim_cpl, ctx);
5656 : :
5657 : : /* Clear metadata space */
5658 : 953 : bs_batch_write_zeroes_dev(batch, 0, num_md_lba);
5659 : :
5660 : 953 : lba = num_md_lba;
5661 : 953 : lba_count = ctx->bs->dev->blockcnt - lba;
5662 [ + - + ]: 953 : switch (opts.clear_method) {
5663 : 921 : case BS_CLEAR_WITH_UNMAP:
5664 : : /* Trim data clusters */
5665 : 921 : bs_batch_unmap_dev(batch, lba, lba_count);
5666 : 921 : break;
5667 : 0 : case BS_CLEAR_WITH_WRITE_ZEROES:
5668 : : /* Write_zeroes to data clusters */
5669 : 0 : bs_batch_write_zeroes_dev(batch, lba, lba_count);
5670 : 0 : break;
5671 : 32 : case BS_CLEAR_WITH_NONE:
5672 : : default:
5673 : 32 : break;
5674 : : }
5675 : :
5676 : 953 : bs_batch_close(batch);
5677 : : }
5678 : :
5679 : : /* END spdk_bs_init */
5680 : :
5681 : : /* START spdk_bs_destroy */
5682 : :
5683 : : static void
5684 : 10 : bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5685 : : {
5686 : 10 : struct spdk_bs_load_ctx *ctx = cb_arg;
5687 : 10 : struct spdk_blob_store *bs = ctx->bs;
5688 : :
5689 : : /*
5690 : : * We need to defer calling bs_call_cpl() until after
5691 : : * dev destruction, so tuck these away for later use.
5692 : : */
5693 : 10 : bs->unload_err = bserrno;
5694 : 10 : memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
5695 : 10 : seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
5696 : :
5697 : 10 : bs_sequence_finish(seq, bserrno);
5698 : :
5699 : 10 : bs_free(bs);
5700 : 10 : free(ctx);
5701 : 10 : }
5702 : :
5703 : : void
5704 : 10 : spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn,
5705 : : void *cb_arg)
5706 : : {
5707 : 10 : struct spdk_bs_cpl cpl;
5708 : : spdk_bs_sequence_t *seq;
5709 : : struct spdk_bs_load_ctx *ctx;
5710 : :
5711 [ - + ]: 10 : SPDK_DEBUGLOG(blob, "Destroying blobstore\n");
5712 : :
5713 [ - + ]: 10 : if (!RB_EMPTY(&bs->open_blobs)) {
5714 : 0 : SPDK_ERRLOG("Blobstore still has open blobs\n");
5715 : 0 : cb_fn(cb_arg, -EBUSY);
5716 : 0 : return;
5717 : : }
5718 : :
5719 : 10 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5720 : 10 : cpl.u.bs_basic.cb_fn = cb_fn;
5721 : 10 : cpl.u.bs_basic.cb_arg = cb_arg;
5722 : :
5723 : 10 : ctx = calloc(1, sizeof(*ctx));
5724 [ - + ]: 10 : if (!ctx) {
5725 : 0 : cb_fn(cb_arg, -ENOMEM);
5726 : 0 : return;
5727 : : }
5728 : :
5729 : 10 : ctx->bs = bs;
5730 : :
5731 : 10 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5732 [ - + ]: 10 : if (!seq) {
5733 : 0 : free(ctx);
5734 : 0 : cb_fn(cb_arg, -ENOMEM);
5735 : 0 : return;
5736 : : }
5737 : :
5738 : : /* Write zeroes to the super block */
5739 : 10 : bs_sequence_write_zeroes_dev(seq,
5740 : : bs_page_to_lba(bs, 0),
5741 : : bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)),
5742 : : bs_destroy_trim_cpl, ctx);
5743 : : }
5744 : :
5745 : : /* END spdk_bs_destroy */
5746 : :
5747 : : /* START spdk_bs_unload */
5748 : :
5749 : : static void
5750 : 1321 : bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno)
5751 : : {
5752 : 1321 : spdk_bs_sequence_t *seq = ctx->seq;
5753 : :
5754 : 1321 : spdk_free(ctx->super);
5755 : :
5756 : : /*
5757 : : * We need to defer calling bs_call_cpl() until after
5758 : : * dev destruction, so tuck these away for later use.
5759 : : */
5760 : 1321 : ctx->bs->unload_err = bserrno;
5761 : 1321 : memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
5762 : 1321 : seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
5763 : :
5764 : 1321 : bs_sequence_finish(seq, bserrno);
5765 : :
5766 : 1321 : bs_free(ctx->bs);
5767 : 1321 : free(ctx);
5768 : 1321 : }
5769 : :
5770 : : static void
5771 : 1321 : bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5772 : : {
5773 : 1321 : struct spdk_bs_load_ctx *ctx = cb_arg;
5774 : :
5775 : 1321 : bs_unload_finish(ctx, bserrno);
5776 : 1321 : }
5777 : :
5778 : : static void
5779 : 1321 : bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5780 : : {
5781 : 1321 : struct spdk_bs_load_ctx *ctx = cb_arg;
5782 : :
5783 : 1321 : spdk_free(ctx->mask);
5784 : :
5785 [ - + ]: 1321 : if (bserrno != 0) {
5786 : 0 : bs_unload_finish(ctx, bserrno);
5787 : 0 : return;
5788 : : }
5789 : :
5790 : 1321 : ctx->super->clean = 1;
5791 : :
5792 : 1321 : bs_write_super(seq, ctx->bs, ctx->super, bs_unload_write_super_cpl, ctx);
5793 : : }
5794 : :
5795 : : static void
5796 : 1321 : bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5797 : : {
5798 : 1321 : struct spdk_bs_load_ctx *ctx = cb_arg;
5799 : :
5800 : 1321 : spdk_free(ctx->mask);
5801 : 1321 : ctx->mask = NULL;
5802 : :
5803 [ - + ]: 1321 : if (bserrno != 0) {
5804 : 0 : bs_unload_finish(ctx, bserrno);
5805 : 0 : return;
5806 : : }
5807 : :
5808 : 1321 : bs_write_used_clusters(seq, ctx, bs_unload_write_used_clusters_cpl);
5809 : : }
5810 : :
5811 : : static void
5812 : 1321 : bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5813 : : {
5814 : 1321 : struct spdk_bs_load_ctx *ctx = cb_arg;
5815 : :
5816 : 1321 : spdk_free(ctx->mask);
5817 : 1321 : ctx->mask = NULL;
5818 : :
5819 [ - + ]: 1321 : if (bserrno != 0) {
5820 : 0 : bs_unload_finish(ctx, bserrno);
5821 : 0 : return;
5822 : : }
5823 : :
5824 : 1321 : bs_write_used_blobids(seq, ctx, bs_unload_write_used_blobids_cpl);
5825 : : }
5826 : :
5827 : : static void
5828 : 1321 : bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5829 : : {
5830 : 1321 : struct spdk_bs_load_ctx *ctx = cb_arg;
5831 : : int rc;
5832 : :
5833 [ - + ]: 1321 : if (bserrno != 0) {
5834 : 0 : bs_unload_finish(ctx, bserrno);
5835 : 0 : return;
5836 : : }
5837 : :
5838 : 1321 : rc = bs_super_validate(ctx->super, ctx->bs);
5839 [ - + ]: 1321 : if (rc != 0) {
5840 : 0 : bs_unload_finish(ctx, rc);
5841 : 0 : return;
5842 : : }
5843 : :
5844 : 1321 : bs_write_used_md(seq, cb_arg, bs_unload_write_used_pages_cpl);
5845 : : }
5846 : :
5847 : : void
5848 : 1337 : spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
5849 : : {
5850 : 1333 : struct spdk_bs_cpl cpl;
5851 : : struct spdk_bs_load_ctx *ctx;
5852 : :
5853 [ - + ]: 1337 : SPDK_DEBUGLOG(blob, "Syncing blobstore\n");
5854 : :
5855 : : /*
5856 : : * If external snapshot channels are being destroyed while the blobstore is unloaded, the
5857 : : * unload is deferred until after the channel destruction completes.
5858 : : */
5859 [ + + ]: 1337 : if (bs->esnap_channels_unloading != 0) {
5860 [ - + ]: 8 : if (bs->esnap_unload_cb_fn != NULL) {
5861 : 0 : SPDK_ERRLOG("Blobstore unload in progress\n");
5862 : 0 : cb_fn(cb_arg, -EBUSY);
5863 : 0 : return;
5864 : : }
5865 [ - + ]: 8 : SPDK_DEBUGLOG(blob_esnap, "Blobstore unload deferred: %" PRIu32
5866 : : " esnap clones are unloading\n", bs->esnap_channels_unloading);
5867 : 8 : bs->esnap_unload_cb_fn = cb_fn;
5868 : 8 : bs->esnap_unload_cb_arg = cb_arg;
5869 : 8 : return;
5870 : : }
5871 [ + + ]: 1329 : if (bs->esnap_unload_cb_fn != NULL) {
5872 [ - + ]: 8 : SPDK_DEBUGLOG(blob_esnap, "Blobstore deferred unload progressing\n");
5873 [ - + ]: 8 : assert(bs->esnap_unload_cb_fn == cb_fn);
5874 [ - + ]: 8 : assert(bs->esnap_unload_cb_arg == cb_arg);
5875 : 8 : bs->esnap_unload_cb_fn = NULL;
5876 : 8 : bs->esnap_unload_cb_arg = NULL;
5877 : : }
5878 : :
5879 [ + + ]: 1329 : if (!RB_EMPTY(&bs->open_blobs)) {
5880 : 8 : SPDK_ERRLOG("Blobstore still has open blobs\n");
5881 : 8 : cb_fn(cb_arg, -EBUSY);
5882 : 8 : return;
5883 : : }
5884 : :
5885 : 1321 : ctx = calloc(1, sizeof(*ctx));
5886 [ - + ]: 1321 : if (!ctx) {
5887 : 0 : cb_fn(cb_arg, -ENOMEM);
5888 : 0 : return;
5889 : : }
5890 : :
5891 : 1321 : ctx->bs = bs;
5892 : :
5893 : 1321 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
5894 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
5895 [ - + ]: 1321 : if (!ctx->super) {
5896 : 0 : free(ctx);
5897 : 0 : cb_fn(cb_arg, -ENOMEM);
5898 : 0 : return;
5899 : : }
5900 : :
5901 : 1321 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5902 : 1321 : cpl.u.bs_basic.cb_fn = cb_fn;
5903 : 1321 : cpl.u.bs_basic.cb_arg = cb_arg;
5904 : :
5905 : 1321 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
5906 [ - + ]: 1321 : if (!ctx->seq) {
5907 : 0 : spdk_free(ctx->super);
5908 : 0 : free(ctx);
5909 : 0 : cb_fn(cb_arg, -ENOMEM);
5910 : 0 : return;
5911 : : }
5912 : :
5913 : : /* Read super block */
5914 : 1321 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
5915 : 1321 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
5916 : : bs_unload_read_super_cpl, ctx);
5917 : : }
5918 : :
5919 : : /* END spdk_bs_unload */
5920 : :
5921 : : /* START spdk_bs_set_super */
5922 : :
5923 : : struct spdk_bs_set_super_ctx {
5924 : : struct spdk_blob_store *bs;
5925 : : struct spdk_bs_super_block *super;
5926 : : };
5927 : :
5928 : : static void
5929 : 25 : bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5930 : : {
5931 : 25 : struct spdk_bs_set_super_ctx *ctx = cb_arg;
5932 : :
5933 [ - + ]: 25 : if (bserrno != 0) {
5934 : 0 : SPDK_ERRLOG("Unable to write to super block of blobstore\n");
5935 : : }
5936 : :
5937 : 25 : spdk_free(ctx->super);
5938 : :
5939 : 25 : bs_sequence_finish(seq, bserrno);
5940 : :
5941 : 25 : free(ctx);
5942 : 25 : }
5943 : :
5944 : : static void
5945 : 25 : bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5946 : : {
5947 : 25 : struct spdk_bs_set_super_ctx *ctx = cb_arg;
5948 : : int rc;
5949 : :
5950 [ - + ]: 25 : if (bserrno != 0) {
5951 : 0 : SPDK_ERRLOG("Unable to read super block of blobstore\n");
5952 : 0 : spdk_free(ctx->super);
5953 : 0 : bs_sequence_finish(seq, bserrno);
5954 : 0 : free(ctx);
5955 : 0 : return;
5956 : : }
5957 : :
5958 : 25 : rc = bs_super_validate(ctx->super, ctx->bs);
5959 [ - + ]: 25 : if (rc != 0) {
5960 : 0 : SPDK_ERRLOG("Not a valid super block\n");
5961 : 0 : spdk_free(ctx->super);
5962 : 0 : bs_sequence_finish(seq, rc);
5963 : 0 : free(ctx);
5964 : 0 : return;
5965 : : }
5966 : :
5967 : 25 : bs_write_super(seq, ctx->bs, ctx->super, bs_set_super_write_cpl, ctx);
5968 : : }
5969 : :
5970 : : void
5971 : 25 : spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
5972 : : spdk_bs_op_complete cb_fn, void *cb_arg)
5973 : : {
5974 : 21 : struct spdk_bs_cpl cpl;
5975 : : spdk_bs_sequence_t *seq;
5976 : : struct spdk_bs_set_super_ctx *ctx;
5977 : :
5978 [ - + ]: 25 : SPDK_DEBUGLOG(blob, "Setting super blob id on blobstore\n");
5979 : :
5980 : 25 : ctx = calloc(1, sizeof(*ctx));
5981 [ - + ]: 25 : if (!ctx) {
5982 : 0 : cb_fn(cb_arg, -ENOMEM);
5983 : 0 : return;
5984 : : }
5985 : :
5986 : 25 : ctx->bs = bs;
5987 : :
5988 : 25 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
5989 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
5990 [ - + ]: 25 : if (!ctx->super) {
5991 : 0 : free(ctx);
5992 : 0 : cb_fn(cb_arg, -ENOMEM);
5993 : 0 : return;
5994 : : }
5995 : :
5996 : 25 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5997 : 25 : cpl.u.bs_basic.cb_fn = cb_fn;
5998 : 25 : cpl.u.bs_basic.cb_arg = cb_arg;
5999 : :
6000 : 25 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
6001 [ - + ]: 25 : if (!seq) {
6002 : 0 : spdk_free(ctx->super);
6003 : 0 : free(ctx);
6004 : 0 : cb_fn(cb_arg, -ENOMEM);
6005 : 0 : return;
6006 : : }
6007 : :
6008 : 25 : bs->super_blob = blobid;
6009 : :
6010 : : /* Read super block */
6011 : 25 : bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
6012 : 25 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
6013 : : bs_set_super_read_cpl, ctx);
6014 : : }
6015 : :
6016 : : /* END spdk_bs_set_super */
6017 : :
6018 : : void
6019 : 30 : spdk_bs_get_super(struct spdk_blob_store *bs,
6020 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6021 : : {
6022 [ + + ]: 30 : if (bs->super_blob == SPDK_BLOBID_INVALID) {
6023 : 8 : cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
6024 : : } else {
6025 : 22 : cb_fn(cb_arg, bs->super_blob, 0);
6026 : : }
6027 : 30 : }
6028 : :
6029 : : uint64_t
6030 : 325 : spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
6031 : : {
6032 : 325 : return bs->cluster_sz;
6033 : : }
6034 : :
6035 : : uint64_t
6036 : 136 : spdk_bs_get_page_size(struct spdk_blob_store *bs)
6037 : : {
6038 : 136 : return SPDK_BS_PAGE_SIZE;
6039 : : }
6040 : :
6041 : : uint64_t
6042 : 1515 : spdk_bs_get_io_unit_size(struct spdk_blob_store *bs)
6043 : : {
6044 : 1515 : return bs->io_unit_size;
6045 : : }
6046 : :
6047 : : uint64_t
6048 : 1080 : spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
6049 : : {
6050 : 1080 : return bs->num_free_clusters;
6051 : : }
6052 : :
6053 : : uint64_t
6054 : 184 : spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs)
6055 : : {
6056 : 184 : return bs->total_data_clusters;
6057 : : }
6058 : :
6059 : : static int
6060 : 3313 : bs_register_md_thread(struct spdk_blob_store *bs)
6061 : : {
6062 : 3313 : bs->md_channel = spdk_get_io_channel(bs);
6063 [ - + ]: 3313 : if (!bs->md_channel) {
6064 : 0 : SPDK_ERRLOG("Failed to get IO channel.\n");
6065 : 0 : return -1;
6066 : : }
6067 : :
6068 : 3313 : return 0;
6069 : : }
6070 : :
6071 : : static int
6072 : 3313 : bs_unregister_md_thread(struct spdk_blob_store *bs)
6073 : : {
6074 : 3313 : spdk_put_io_channel(bs->md_channel);
6075 : :
6076 : 3313 : return 0;
6077 : : }
6078 : :
6079 : : spdk_blob_id
6080 : 1174 : spdk_blob_get_id(struct spdk_blob *blob)
6081 : : {
6082 [ - + ]: 1174 : assert(blob != NULL);
6083 : :
6084 : 1174 : return blob->id;
6085 : : }
6086 : :
6087 : : uint64_t
6088 : 60 : spdk_blob_get_num_pages(struct spdk_blob *blob)
6089 : : {
6090 [ - + ]: 60 : assert(blob != NULL);
6091 : :
6092 : 60 : return bs_cluster_to_page(blob->bs, blob->active.num_clusters);
6093 : : }
6094 : :
6095 : : uint64_t
6096 : 60 : spdk_blob_get_num_io_units(struct spdk_blob *blob)
6097 : : {
6098 [ - + ]: 60 : assert(blob != NULL);
6099 : :
6100 : 60 : return spdk_blob_get_num_pages(blob) * bs_io_unit_per_page(blob->bs);
6101 : : }
6102 : :
6103 : : uint64_t
6104 : 1159 : spdk_blob_get_num_clusters(struct spdk_blob *blob)
6105 : : {
6106 [ - + ]: 1159 : assert(blob != NULL);
6107 : :
6108 : 1159 : return blob->active.num_clusters;
6109 : : }
6110 : :
6111 : : uint64_t
6112 : 668 : spdk_blob_get_num_allocated_clusters(struct spdk_blob *blob)
6113 : : {
6114 [ - + ]: 668 : assert(blob != NULL);
6115 : :
6116 : 668 : return blob->active.num_allocated_clusters;
6117 : : }
6118 : :
6119 : : static uint64_t
6120 : 60 : blob_find_io_unit(struct spdk_blob *blob, uint64_t offset, bool is_allocated)
6121 : : {
6122 : 60 : uint64_t blob_io_unit_num = spdk_blob_get_num_io_units(blob);
6123 : :
6124 [ + + ]: 118 : while (offset < blob_io_unit_num) {
6125 [ + + ]: 108 : if (bs_io_unit_is_allocated(blob, offset) == is_allocated) {
6126 : 50 : return offset;
6127 : : }
6128 : :
6129 : 58 : offset += bs_num_io_units_to_cluster_boundary(blob, offset);
6130 : : }
6131 : :
6132 : 10 : return UINT64_MAX;
6133 : : }
6134 : :
6135 : : uint64_t
6136 : 30 : spdk_blob_get_next_allocated_io_unit(struct spdk_blob *blob, uint64_t offset)
6137 : : {
6138 : 30 : return blob_find_io_unit(blob, offset, true);
6139 : : }
6140 : :
6141 : : uint64_t
6142 : 30 : spdk_blob_get_next_unallocated_io_unit(struct spdk_blob *blob, uint64_t offset)
6143 : : {
6144 : 30 : return blob_find_io_unit(blob, offset, false);
6145 : : }
6146 : :
6147 : : /* START spdk_bs_create_blob */
6148 : :
6149 : : static void
6150 : 3772 : bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
6151 : : {
6152 : 3772 : struct spdk_blob *blob = cb_arg;
6153 : 3772 : uint32_t page_idx = bs_blobid_to_page(blob->id);
6154 : :
6155 [ - + ]: 3772 : if (bserrno != 0) {
6156 : 0 : spdk_spin_lock(&blob->bs->used_lock);
6157 : 0 : spdk_bit_array_clear(blob->bs->used_blobids, page_idx);
6158 : 0 : bs_release_md_page(blob->bs, page_idx);
6159 : 0 : spdk_spin_unlock(&blob->bs->used_lock);
6160 : : }
6161 : :
6162 : 3772 : blob_free(blob);
6163 : :
6164 : 3772 : bs_sequence_finish(seq, bserrno);
6165 : 3772 : }
6166 : :
6167 : : static int
6168 : 7584 : blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs,
6169 : : bool internal)
6170 : : {
6171 : : uint64_t i;
6172 : 7584 : size_t value_len = 0;
6173 : : int rc;
6174 : 7584 : const void *value = NULL;
6175 [ + + + + ]: 7584 : if (xattrs->count > 0 && xattrs->get_value == NULL) {
6176 : 16 : return -EINVAL;
6177 : : }
6178 [ + + ]: 8226 : for (i = 0; i < xattrs->count; i++) {
6179 : 666 : xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len);
6180 [ + + - + ]: 666 : if (value == NULL || value_len == 0) {
6181 : 8 : return -EINVAL;
6182 : : }
6183 : 658 : rc = blob_set_xattr(blob, xattrs->names[i], value, value_len, internal);
6184 [ - + ]: 658 : if (rc < 0) {
6185 : 0 : return rc;
6186 : : }
6187 : : }
6188 : 7560 : return 0;
6189 : : }
6190 : :
6191 : : static void
6192 : 3731 : blob_opts_copy(const struct spdk_blob_opts *src, struct spdk_blob_opts *dst)
6193 : : {
6194 : : #define FIELD_OK(field) \
6195 : : offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size
6196 : :
6197 : : #define SET_FIELD(field) \
6198 : : if (FIELD_OK(field)) { \
6199 : : dst->field = src->field; \
6200 : : } \
6201 : :
6202 [ + - ]: 3731 : SET_FIELD(num_clusters);
6203 [ + - ]: 3731 : SET_FIELD(thin_provision);
6204 [ + - ]: 3731 : SET_FIELD(clear_method);
6205 : :
6206 [ + - ]: 3731 : if (FIELD_OK(xattrs)) {
6207 : 3731 : memcpy(&dst->xattrs, &src->xattrs, sizeof(src->xattrs));
6208 : : }
6209 : :
6210 [ + - ]: 3731 : SET_FIELD(use_extent_table);
6211 [ + - ]: 3731 : SET_FIELD(esnap_id);
6212 [ + - ]: 3731 : SET_FIELD(esnap_id_len);
6213 : :
6214 : 3731 : dst->opts_size = src->opts_size;
6215 : :
6216 : : /* You should not remove this statement, but need to update the assert statement
6217 : : * if you add a new field, and also add a corresponding SET_FIELD statement */
6218 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_opts) == 80, "Incorrect size");
6219 : :
6220 : : #undef FIELD_OK
6221 : : #undef SET_FIELD
6222 : 3731 : }
6223 : :
6224 : : static void
6225 : 3804 : bs_create_blob(struct spdk_blob_store *bs,
6226 : : const struct spdk_blob_opts *opts,
6227 : : const struct spdk_blob_xattr_opts *internal_xattrs,
6228 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6229 : : {
6230 : : struct spdk_blob *blob;
6231 : : uint32_t page_idx;
6232 : 3796 : struct spdk_bs_cpl cpl;
6233 : 3796 : struct spdk_blob_opts opts_local;
6234 : 3796 : struct spdk_blob_xattr_opts internal_xattrs_default;
6235 : : spdk_bs_sequence_t *seq;
6236 : : spdk_blob_id id;
6237 : : int rc;
6238 : :
6239 [ - + ]: 3804 : assert(spdk_get_thread() == bs->md_thread);
6240 : :
6241 : 3804 : spdk_spin_lock(&bs->used_lock);
6242 : 3804 : page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
6243 [ - + ]: 3804 : if (page_idx == UINT32_MAX) {
6244 : 0 : spdk_spin_unlock(&bs->used_lock);
6245 : 0 : cb_fn(cb_arg, 0, -ENOMEM);
6246 : 8 : return;
6247 : : }
6248 : 3804 : spdk_bit_array_set(bs->used_blobids, page_idx);
6249 : 3804 : bs_claim_md_page(bs, page_idx);
6250 : 3804 : spdk_spin_unlock(&bs->used_lock);
6251 : :
6252 : 3804 : id = bs_page_to_blobid(page_idx);
6253 : :
6254 [ - + ]: 3804 : SPDK_DEBUGLOG(blob, "Creating blob with id 0x%" PRIx64 " at page %u\n", id, page_idx);
6255 : :
6256 : 3804 : spdk_blob_opts_init(&opts_local, sizeof(opts_local));
6257 [ + + ]: 3804 : if (opts) {
6258 : 3731 : blob_opts_copy(opts, &opts_local);
6259 : : }
6260 : :
6261 : 3804 : blob = blob_alloc(bs, id);
6262 [ - + ]: 3804 : if (!blob) {
6263 : 0 : rc = -ENOMEM;
6264 : 0 : goto error;
6265 : : }
6266 : :
6267 : 3804 : blob->use_extent_table = opts_local.use_extent_table;
6268 [ + + ]: 3804 : if (blob->use_extent_table) {
6269 : 1956 : blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE;
6270 : : }
6271 : :
6272 [ + + ]: 3804 : if (!internal_xattrs) {
6273 : 3264 : blob_xattrs_init(&internal_xattrs_default);
6274 : 3264 : internal_xattrs = &internal_xattrs_default;
6275 : : }
6276 : :
6277 : 3804 : rc = blob_set_xattrs(blob, &opts_local.xattrs, false);
6278 [ + + ]: 3804 : if (rc < 0) {
6279 : 24 : goto error;
6280 : : }
6281 : :
6282 : 3780 : rc = blob_set_xattrs(blob, internal_xattrs, true);
6283 [ - + ]: 3780 : if (rc < 0) {
6284 : 0 : goto error;
6285 : : }
6286 : :
6287 [ + + ]: 3780 : if (opts_local.thin_provision) {
6288 : 712 : blob_set_thin_provision(blob);
6289 : : }
6290 : :
6291 : 3780 : blob_set_clear_method(blob, opts_local.clear_method);
6292 : :
6293 [ + + ]: 3780 : if (opts_local.esnap_id != NULL) {
6294 [ - + ]: 120 : if (opts_local.esnap_id_len > UINT16_MAX) {
6295 : 0 : SPDK_ERRLOG("esnap id length %" PRIu64 "is too long\n",
6296 : : opts_local.esnap_id_len);
6297 : 0 : rc = -EINVAL;
6298 : 0 : goto error;
6299 : :
6300 : : }
6301 : 120 : blob_set_thin_provision(blob);
6302 : 120 : blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
6303 : 120 : rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID,
6304 : 120 : opts_local.esnap_id, opts_local.esnap_id_len, true);
6305 [ - + ]: 120 : if (rc != 0) {
6306 : 0 : goto error;
6307 : : }
6308 : : }
6309 : :
6310 : 3780 : rc = blob_resize(blob, opts_local.num_clusters);
6311 [ + + ]: 3780 : if (rc < 0) {
6312 : 8 : goto error;
6313 : : }
6314 : 3772 : cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
6315 : 3772 : cpl.u.blobid.cb_fn = cb_fn;
6316 : 3772 : cpl.u.blobid.cb_arg = cb_arg;
6317 : 3772 : cpl.u.blobid.blobid = blob->id;
6318 : :
6319 : 3772 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
6320 [ - + ]: 3772 : if (!seq) {
6321 : 0 : rc = -ENOMEM;
6322 : 0 : goto error;
6323 : : }
6324 : :
6325 : 3772 : blob_persist(seq, blob, bs_create_blob_cpl, blob);
6326 : 3772 : return;
6327 : :
6328 : 32 : error:
6329 : 32 : SPDK_ERRLOG("Failed to create blob: %s, size in clusters/size: %lu (clusters)\n",
6330 : : spdk_strerror(rc), opts_local.num_clusters);
6331 [ + - ]: 32 : if (blob != NULL) {
6332 : 32 : blob_free(blob);
6333 : : }
6334 : 32 : spdk_spin_lock(&bs->used_lock);
6335 : 32 : spdk_bit_array_clear(bs->used_blobids, page_idx);
6336 : 32 : bs_release_md_page(bs, page_idx);
6337 : 32 : spdk_spin_unlock(&bs->used_lock);
6338 : 32 : cb_fn(cb_arg, 0, rc);
6339 : : }
6340 : :
6341 : : void
6342 : 41 : spdk_bs_create_blob(struct spdk_blob_store *bs,
6343 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6344 : : {
6345 : 41 : bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg);
6346 : 41 : }
6347 : :
6348 : : void
6349 : 3207 : spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts,
6350 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6351 : : {
6352 : 3207 : bs_create_blob(bs, opts, NULL, cb_fn, cb_arg);
6353 : 3207 : }
6354 : :
6355 : : /* END spdk_bs_create_blob */
6356 : :
6357 : : /* START blob_cleanup */
6358 : :
6359 : : struct spdk_clone_snapshot_ctx {
6360 : : struct spdk_bs_cpl cpl;
6361 : : int bserrno;
6362 : : bool frozen;
6363 : :
6364 : : struct spdk_io_channel *channel;
6365 : :
6366 : : /* Current cluster for inflate operation */
6367 : : uint64_t cluster;
6368 : :
6369 : : /* For inflation force allocation of all unallocated clusters and remove
6370 : : * thin-provisioning. Otherwise only decouple parent and keep clone thin. */
6371 : : bool allocate_all;
6372 : :
6373 : : struct {
6374 : : spdk_blob_id id;
6375 : : struct spdk_blob *blob;
6376 : : bool md_ro;
6377 : : } original;
6378 : : struct {
6379 : : spdk_blob_id id;
6380 : : struct spdk_blob *blob;
6381 : : } new;
6382 : :
6383 : : /* xattrs specified for snapshot/clones only. They have no impact on
6384 : : * the original blobs xattrs. */
6385 : : const struct spdk_blob_xattr_opts *xattrs;
6386 : : };
6387 : :
6388 : : static void
6389 : 680 : bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno)
6390 : : {
6391 : 680 : struct spdk_clone_snapshot_ctx *ctx = cb_arg;
6392 : 680 : struct spdk_bs_cpl *cpl = &ctx->cpl;
6393 : :
6394 [ + + ]: 680 : if (bserrno != 0) {
6395 [ - + ]: 12 : if (ctx->bserrno != 0) {
6396 : 0 : SPDK_ERRLOG("Cleanup error %d\n", bserrno);
6397 : : } else {
6398 : 12 : ctx->bserrno = bserrno;
6399 : : }
6400 : : }
6401 : :
6402 [ + + - ]: 680 : switch (cpl->type) {
6403 : 560 : case SPDK_BS_CPL_TYPE_BLOBID:
6404 : 560 : cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno);
6405 : 560 : break;
6406 : 120 : case SPDK_BS_CPL_TYPE_BLOB_BASIC:
6407 : 120 : cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
6408 : 120 : break;
6409 : 0 : default:
6410 : 0 : SPDK_UNREACHABLE();
6411 : : break;
6412 : : }
6413 : :
6414 : 680 : free(ctx);
6415 : 680 : }
6416 : :
6417 : : static void
6418 : 652 : bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
6419 : : {
6420 : 652 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6421 : 652 : struct spdk_blob *origblob = ctx->original.blob;
6422 : :
6423 [ - + ]: 652 : if (bserrno != 0) {
6424 [ # # ]: 0 : if (ctx->bserrno != 0) {
6425 : 0 : SPDK_ERRLOG("Unfreeze error %d\n", bserrno);
6426 : : } else {
6427 : 0 : ctx->bserrno = bserrno;
6428 : : }
6429 : : }
6430 : :
6431 : 652 : ctx->original.id = origblob->id;
6432 : 652 : origblob->locked_operation_in_progress = false;
6433 : :
6434 : : /* Revert md_ro to original state */
6435 : 652 : origblob->md_ro = ctx->original.md_ro;
6436 : :
6437 : 652 : spdk_blob_close(origblob, bs_clone_snapshot_cleanup_finish, ctx);
6438 : 652 : }
6439 : :
6440 : : static void
6441 : 652 : bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno)
6442 : : {
6443 : 652 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6444 : 652 : struct spdk_blob *origblob = ctx->original.blob;
6445 : :
6446 [ + + ]: 652 : if (bserrno != 0) {
6447 [ + + ]: 48 : if (ctx->bserrno != 0) {
6448 : 8 : SPDK_ERRLOG("Cleanup error %d\n", bserrno);
6449 : : } else {
6450 : 40 : ctx->bserrno = bserrno;
6451 : : }
6452 : : }
6453 : :
6454 [ + + ]: 652 : if (ctx->frozen) {
6455 : : /* Unfreeze any outstanding I/O */
6456 : 418 : blob_unfreeze_io(origblob, bs_snapshot_unfreeze_cpl, ctx);
6457 : : } else {
6458 : 234 : bs_snapshot_unfreeze_cpl(ctx, 0);
6459 : : }
6460 : :
6461 : 652 : }
6462 : :
6463 : : static void
6464 : 8 : bs_clone_snapshot_newblob_cleanup(struct spdk_clone_snapshot_ctx *ctx, int bserrno)
6465 : : {
6466 : 8 : struct spdk_blob *newblob = ctx->new.blob;
6467 : :
6468 [ + - ]: 8 : if (bserrno != 0) {
6469 [ - + ]: 8 : if (ctx->bserrno != 0) {
6470 : 0 : SPDK_ERRLOG("Cleanup error %d\n", bserrno);
6471 : : } else {
6472 : 8 : ctx->bserrno = bserrno;
6473 : : }
6474 : : }
6475 : :
6476 : 8 : ctx->new.id = newblob->id;
6477 : 8 : spdk_blob_close(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
6478 : 8 : }
6479 : :
6480 : : /* END blob_cleanup */
6481 : :
6482 : : /* START spdk_bs_create_snapshot */
6483 : :
6484 : : static void
6485 : 434 : bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2)
6486 : : {
6487 : : uint64_t *cluster_temp;
6488 : : uint64_t num_allocated_clusters_temp;
6489 : : uint32_t *extent_page_temp;
6490 : :
6491 : 434 : cluster_temp = blob1->active.clusters;
6492 : 434 : blob1->active.clusters = blob2->active.clusters;
6493 : 434 : blob2->active.clusters = cluster_temp;
6494 : :
6495 : 434 : num_allocated_clusters_temp = blob1->active.num_allocated_clusters;
6496 : 434 : blob1->active.num_allocated_clusters = blob2->active.num_allocated_clusters;
6497 : 434 : blob2->active.num_allocated_clusters = num_allocated_clusters_temp;
6498 : :
6499 : 434 : extent_page_temp = blob1->active.extent_pages;
6500 : 434 : blob1->active.extent_pages = blob2->active.extent_pages;
6501 : 434 : blob2->active.extent_pages = extent_page_temp;
6502 : 434 : }
6503 : :
6504 : : /* Copies an internal xattr */
6505 : : static int
6506 : 40 : bs_snapshot_copy_xattr(struct spdk_blob *toblob, struct spdk_blob *fromblob, const char *name)
6507 : : {
6508 : 40 : const void *val = NULL;
6509 : 40 : size_t len;
6510 : : int bserrno;
6511 : :
6512 : 40 : bserrno = blob_get_xattr_value(fromblob, name, &val, &len, true);
6513 [ - + ]: 40 : if (bserrno != 0) {
6514 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " missing %s XATTR\n", fromblob->id, name);
6515 : 0 : return bserrno;
6516 : : }
6517 : :
6518 : 40 : bserrno = blob_set_xattr(toblob, name, val, len, true);
6519 [ - + ]: 40 : if (bserrno != 0) {
6520 : 0 : SPDK_ERRLOG("could not set %s XATTR on blob 0x%" PRIx64 "\n",
6521 : : name, toblob->id);
6522 : 0 : return bserrno;
6523 : : }
6524 : 40 : return 0;
6525 : : }
6526 : :
6527 : : static void
6528 : 410 : bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno)
6529 : : {
6530 : 410 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6531 : 410 : struct spdk_blob *origblob = ctx->original.blob;
6532 : 410 : struct spdk_blob *newblob = ctx->new.blob;
6533 : :
6534 [ + + ]: 410 : if (bserrno != 0) {
6535 : 8 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6536 [ - + ]: 8 : if (blob_is_esnap_clone(newblob)) {
6537 : 0 : bs_snapshot_copy_xattr(origblob, newblob, BLOB_EXTERNAL_SNAPSHOT_ID);
6538 : 0 : origblob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
6539 : : }
6540 : 8 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6541 : 8 : return;
6542 : : }
6543 : :
6544 : : /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */
6545 : 402 : bserrno = blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true);
6546 [ - + ]: 402 : if (bserrno != 0) {
6547 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6548 : 0 : return;
6549 : : }
6550 : :
6551 : 402 : bs_blob_list_add(ctx->original.blob);
6552 : :
6553 : 402 : spdk_blob_set_read_only(newblob);
6554 : :
6555 : : /* sync snapshot metadata */
6556 : 402 : spdk_blob_sync_md(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
6557 : : }
6558 : :
6559 : : static void
6560 : 418 : bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno)
6561 : : {
6562 : 418 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6563 : 418 : struct spdk_blob *origblob = ctx->original.blob;
6564 : 418 : struct spdk_blob *newblob = ctx->new.blob;
6565 : :
6566 [ + + ]: 418 : if (bserrno != 0) {
6567 : : /* return cluster map back to original */
6568 : 8 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6569 : :
6570 : : /* Newblob md sync failed. Valid clusters are only present in origblob.
6571 : : * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occurred.
6572 : : * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */
6573 : 8 : blob_set_thin_provision(newblob);
6574 [ - + ]: 8 : assert(spdk_mem_all_zero(newblob->active.clusters,
6575 : : newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
6576 [ - + ]: 8 : assert(spdk_mem_all_zero(newblob->active.extent_pages,
6577 : : newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
6578 : :
6579 : 8 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6580 : 8 : return;
6581 : : }
6582 : :
6583 : : /* Set internal xattr for snapshot id */
6584 : 410 : bserrno = blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true);
6585 [ - + ]: 410 : if (bserrno != 0) {
6586 : : /* return cluster map back to original */
6587 : 0 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6588 : 0 : blob_set_thin_provision(newblob);
6589 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6590 : 0 : return;
6591 : : }
6592 : :
6593 : : /* Create new back_bs_dev for snapshot */
6594 : 410 : origblob->back_bs_dev = bs_create_blob_bs_dev(newblob);
6595 [ - + ]: 410 : if (origblob->back_bs_dev == NULL) {
6596 : : /* return cluster map back to original */
6597 : 0 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6598 : 0 : blob_set_thin_provision(newblob);
6599 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL);
6600 : 0 : return;
6601 : : }
6602 : :
6603 : : /* Remove the xattr that references an external snapshot */
6604 [ + + ]: 410 : if (blob_is_esnap_clone(origblob)) {
6605 : 24 : origblob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
6606 : 24 : bserrno = blob_remove_xattr(origblob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
6607 [ - + ]: 24 : if (bserrno != 0) {
6608 [ # # ]: 0 : if (bserrno == -ENOENT) {
6609 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " has no " BLOB_EXTERNAL_SNAPSHOT_ID
6610 : : " xattr to remove\n", origblob->id);
6611 : 0 : assert(false);
6612 : : } else {
6613 : : /* return cluster map back to original */
6614 : 0 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6615 : 0 : blob_set_thin_provision(newblob);
6616 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6617 : 0 : return;
6618 : : }
6619 : : }
6620 : : }
6621 : :
6622 : 410 : bs_blob_list_remove(origblob);
6623 : 410 : origblob->parent_id = newblob->id;
6624 : : /* set clone blob as thin provisioned */
6625 : 410 : blob_set_thin_provision(origblob);
6626 : :
6627 : 410 : bs_blob_list_add(newblob);
6628 : :
6629 : : /* sync clone metadata */
6630 : 410 : spdk_blob_sync_md(origblob, bs_snapshot_origblob_sync_cpl, ctx);
6631 : : }
6632 : :
6633 : : static void
6634 : 418 : bs_snapshot_freeze_cpl(void *cb_arg, int rc)
6635 : : {
6636 : 418 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6637 : 418 : struct spdk_blob *origblob = ctx->original.blob;
6638 : 418 : struct spdk_blob *newblob = ctx->new.blob;
6639 : : int bserrno;
6640 : :
6641 [ - + ]: 418 : if (rc != 0) {
6642 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, rc);
6643 : 0 : return;
6644 : : }
6645 : :
6646 : 418 : ctx->frozen = true;
6647 : :
6648 [ + + ]: 418 : if (blob_is_esnap_clone(origblob)) {
6649 : : /* Clean up any channels associated with the original blob id because future IO will
6650 : : * perform IO using the snapshot blob_id.
6651 : : */
6652 : 24 : blob_esnap_destroy_bs_dev_channels(origblob, false, NULL, NULL);
6653 : : }
6654 [ + - ]: 418 : if (newblob->back_bs_dev) {
6655 : 418 : blob_back_bs_destroy(newblob);
6656 : : }
6657 : : /* set new back_bs_dev for snapshot */
6658 : 418 : newblob->back_bs_dev = origblob->back_bs_dev;
6659 : : /* Set invalid flags from origblob */
6660 : 418 : newblob->invalid_flags = origblob->invalid_flags;
6661 : :
6662 : : /* inherit parent from original blob if set */
6663 : 418 : newblob->parent_id = origblob->parent_id;
6664 [ + + + ]: 418 : switch (origblob->parent_id) {
6665 : 24 : case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
6666 : 24 : bserrno = bs_snapshot_copy_xattr(newblob, origblob, BLOB_EXTERNAL_SNAPSHOT_ID);
6667 [ - + ]: 24 : if (bserrno != 0) {
6668 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6669 : 0 : return;
6670 : : }
6671 : 24 : break;
6672 : 290 : case SPDK_BLOBID_INVALID:
6673 : 290 : break;
6674 : 104 : default:
6675 : : /* Set internal xattr for snapshot id */
6676 : 104 : bserrno = blob_set_xattr(newblob, BLOB_SNAPSHOT,
6677 : 104 : &origblob->parent_id, sizeof(spdk_blob_id), true);
6678 [ - + ]: 104 : if (bserrno != 0) {
6679 : 0 : bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
6680 : 0 : return;
6681 : : }
6682 : : }
6683 : :
6684 : : /* swap cluster maps */
6685 : 418 : bs_snapshot_swap_cluster_maps(newblob, origblob);
6686 : :
6687 : : /* Set the clear method on the new blob to match the original. */
6688 : 418 : blob_set_clear_method(newblob, origblob->clear_method);
6689 : :
6690 : : /* sync snapshot metadata */
6691 : 418 : spdk_blob_sync_md(newblob, bs_snapshot_newblob_sync_cpl, ctx);
6692 : : }
6693 : :
6694 : : static void
6695 : 426 : bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6696 : : {
6697 : 426 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6698 : 426 : struct spdk_blob *origblob = ctx->original.blob;
6699 : 426 : struct spdk_blob *newblob = _blob;
6700 : :
6701 [ + + ]: 426 : if (bserrno != 0) {
6702 : 8 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6703 : 8 : return;
6704 : : }
6705 : :
6706 : 418 : ctx->new.blob = newblob;
6707 [ - + ]: 418 : assert(spdk_blob_is_thin_provisioned(newblob));
6708 [ - + ]: 418 : assert(spdk_mem_all_zero(newblob->active.clusters,
6709 : : newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
6710 [ - + ]: 418 : assert(spdk_mem_all_zero(newblob->active.extent_pages,
6711 : : newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
6712 : :
6713 : 418 : blob_freeze_io(origblob, bs_snapshot_freeze_cpl, ctx);
6714 : : }
6715 : :
6716 : : static void
6717 : 434 : bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
6718 : : {
6719 : 434 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6720 : 434 : struct spdk_blob *origblob = ctx->original.blob;
6721 : :
6722 [ + + ]: 434 : if (bserrno != 0) {
6723 : 8 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6724 : 8 : return;
6725 : : }
6726 : :
6727 : 426 : ctx->new.id = blobid;
6728 : 426 : ctx->cpl.u.blobid.blobid = blobid;
6729 : :
6730 : 426 : spdk_bs_open_blob(origblob->bs, ctx->new.id, bs_snapshot_newblob_open_cpl, ctx);
6731 : : }
6732 : :
6733 : :
6734 : : static void
6735 : 434 : bs_xattr_snapshot(void *arg, const char *name,
6736 : : const void **value, size_t *value_len)
6737 : : {
6738 [ - + ]: 434 : assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0);
6739 : :
6740 : 434 : struct spdk_blob *blob = (struct spdk_blob *)arg;
6741 : 434 : *value = &blob->id;
6742 : 434 : *value_len = sizeof(blob->id);
6743 : 434 : }
6744 : :
6745 : : static void
6746 : 454 : bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6747 : : {
6748 : 454 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6749 : 454 : struct spdk_blob_opts opts;
6750 : 454 : struct spdk_blob_xattr_opts internal_xattrs;
6751 : 454 : char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS };
6752 : :
6753 [ + + ]: 454 : if (bserrno != 0) {
6754 : 12 : bs_clone_snapshot_cleanup_finish(ctx, bserrno);
6755 : 12 : return;
6756 : : }
6757 : :
6758 : 442 : ctx->original.blob = _blob;
6759 : :
6760 [ + + - + ]: 442 : if (_blob->data_ro || _blob->md_ro) {
6761 [ - + ]: 8 : SPDK_DEBUGLOG(blob, "Cannot create snapshot from read only blob with id 0x%"
6762 : : PRIx64 "\n", _blob->id);
6763 : 8 : ctx->bserrno = -EINVAL;
6764 : 8 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6765 : 8 : return;
6766 : : }
6767 : :
6768 [ - + ]: 434 : if (_blob->locked_operation_in_progress) {
6769 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot create snapshot - another operation in progress\n");
6770 : 0 : ctx->bserrno = -EBUSY;
6771 : 0 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6772 : 0 : return;
6773 : : }
6774 : :
6775 : 434 : _blob->locked_operation_in_progress = true;
6776 : :
6777 : 434 : spdk_blob_opts_init(&opts, sizeof(opts));
6778 : 434 : blob_xattrs_init(&internal_xattrs);
6779 : :
6780 : : /* Change the size of new blob to the same as in original blob,
6781 : : * but do not allocate clusters */
6782 : 434 : opts.thin_provision = true;
6783 : 434 : opts.num_clusters = spdk_blob_get_num_clusters(_blob);
6784 : 434 : opts.use_extent_table = _blob->use_extent_table;
6785 : :
6786 : : /* If there are any xattrs specified for snapshot, set them now */
6787 [ + + ]: 434 : if (ctx->xattrs) {
6788 : 10 : memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
6789 : : }
6790 : : /* Set internal xattr SNAPSHOT_IN_PROGRESS */
6791 : 434 : internal_xattrs.count = 1;
6792 : 434 : internal_xattrs.ctx = _blob;
6793 : 434 : internal_xattrs.names = xattrs_names;
6794 : 434 : internal_xattrs.get_value = bs_xattr_snapshot;
6795 : :
6796 : 434 : bs_create_blob(_blob->bs, &opts, &internal_xattrs,
6797 : : bs_snapshot_newblob_create_cpl, ctx);
6798 : : }
6799 : :
6800 : : void
6801 : 454 : spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid,
6802 : : const struct spdk_blob_xattr_opts *snapshot_xattrs,
6803 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6804 : : {
6805 : 454 : struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
6806 : :
6807 [ - + ]: 454 : if (!ctx) {
6808 : 0 : cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
6809 : 0 : return;
6810 : : }
6811 : 454 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
6812 : 454 : ctx->cpl.u.blobid.cb_fn = cb_fn;
6813 : 454 : ctx->cpl.u.blobid.cb_arg = cb_arg;
6814 : 454 : ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
6815 : 454 : ctx->bserrno = 0;
6816 : 454 : ctx->frozen = false;
6817 : 454 : ctx->original.id = blobid;
6818 : 454 : ctx->xattrs = snapshot_xattrs;
6819 : :
6820 : 454 : spdk_bs_open_blob(bs, ctx->original.id, bs_snapshot_origblob_open_cpl, ctx);
6821 : : }
6822 : : /* END spdk_bs_create_snapshot */
6823 : :
6824 : : /* START spdk_bs_create_clone */
6825 : :
6826 : : static void
6827 : 98 : bs_xattr_clone(void *arg, const char *name,
6828 : : const void **value, size_t *value_len)
6829 : : {
6830 [ - + ]: 98 : assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0);
6831 : :
6832 : 98 : struct spdk_blob *blob = (struct spdk_blob *)arg;
6833 : 98 : *value = &blob->id;
6834 : 98 : *value_len = sizeof(blob->id);
6835 : 98 : }
6836 : :
6837 : : static void
6838 : 98 : bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6839 : : {
6840 : 98 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6841 : 98 : struct spdk_blob *clone = _blob;
6842 : :
6843 : 98 : ctx->new.blob = clone;
6844 : 98 : bs_blob_list_add(clone);
6845 : :
6846 : 98 : spdk_blob_close(clone, bs_clone_snapshot_origblob_cleanup, ctx);
6847 : 98 : }
6848 : :
6849 : : static void
6850 : 98 : bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
6851 : : {
6852 : 98 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6853 : :
6854 : 98 : ctx->cpl.u.blobid.blobid = blobid;
6855 : 98 : spdk_bs_open_blob(ctx->original.blob->bs, blobid, bs_clone_newblob_open_cpl, ctx);
6856 : 98 : }
6857 : :
6858 : : static void
6859 : 106 : bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
6860 : : {
6861 : 106 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6862 : 106 : struct spdk_blob_opts opts;
6863 : 106 : struct spdk_blob_xattr_opts internal_xattrs;
6864 : 106 : char *xattr_names[] = { BLOB_SNAPSHOT };
6865 : :
6866 [ - + ]: 106 : if (bserrno != 0) {
6867 : 0 : bs_clone_snapshot_cleanup_finish(ctx, bserrno);
6868 : 0 : return;
6869 : : }
6870 : :
6871 : 106 : ctx->original.blob = _blob;
6872 : 106 : ctx->original.md_ro = _blob->md_ro;
6873 : :
6874 [ + + - + ]: 106 : if (!_blob->data_ro || !_blob->md_ro) {
6875 [ - + ]: 8 : SPDK_DEBUGLOG(blob, "Clone not from read-only blob\n");
6876 : 8 : ctx->bserrno = -EINVAL;
6877 : 8 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6878 : 8 : return;
6879 : : }
6880 : :
6881 [ - + ]: 98 : if (_blob->locked_operation_in_progress) {
6882 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot create clone - another operation in progress\n");
6883 : 0 : ctx->bserrno = -EBUSY;
6884 : 0 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
6885 : 0 : return;
6886 : : }
6887 : :
6888 : 98 : _blob->locked_operation_in_progress = true;
6889 : :
6890 : 98 : spdk_blob_opts_init(&opts, sizeof(opts));
6891 : 98 : blob_xattrs_init(&internal_xattrs);
6892 : :
6893 : 98 : opts.thin_provision = true;
6894 : 98 : opts.num_clusters = spdk_blob_get_num_clusters(_blob);
6895 : 98 : opts.use_extent_table = _blob->use_extent_table;
6896 [ + + ]: 98 : if (ctx->xattrs) {
6897 : 10 : memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
6898 : : }
6899 : :
6900 : : /* Set internal xattr BLOB_SNAPSHOT */
6901 : 98 : internal_xattrs.count = 1;
6902 : 98 : internal_xattrs.ctx = _blob;
6903 : 98 : internal_xattrs.names = xattr_names;
6904 : 98 : internal_xattrs.get_value = bs_xattr_clone;
6905 : :
6906 : 98 : bs_create_blob(_blob->bs, &opts, &internal_xattrs,
6907 : : bs_clone_newblob_create_cpl, ctx);
6908 : : }
6909 : :
6910 : : void
6911 : 106 : spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid,
6912 : : const struct spdk_blob_xattr_opts *clone_xattrs,
6913 : : spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
6914 : : {
6915 : 106 : struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
6916 : :
6917 [ - + ]: 106 : if (!ctx) {
6918 : 0 : cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
6919 : 0 : return;
6920 : : }
6921 : :
6922 : 106 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
6923 : 106 : ctx->cpl.u.blobid.cb_fn = cb_fn;
6924 : 106 : ctx->cpl.u.blobid.cb_arg = cb_arg;
6925 : 106 : ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
6926 : 106 : ctx->bserrno = 0;
6927 : 106 : ctx->xattrs = clone_xattrs;
6928 : 106 : ctx->original.id = blobid;
6929 : :
6930 : 106 : spdk_bs_open_blob(bs, ctx->original.id, bs_clone_origblob_open_cpl, ctx);
6931 : : }
6932 : :
6933 : : /* END spdk_bs_create_clone */
6934 : :
6935 : : /* START spdk_bs_inflate_blob */
6936 : :
6937 : : static void
6938 : 24 : bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno)
6939 : : {
6940 : 24 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
6941 : 24 : struct spdk_blob *_blob = ctx->original.blob;
6942 : :
6943 [ - + ]: 24 : if (bserrno != 0) {
6944 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6945 : 0 : return;
6946 : : }
6947 : :
6948 : : /* Temporarily override md_ro flag for MD modification */
6949 : 24 : _blob->md_ro = false;
6950 : :
6951 : 24 : bserrno = blob_set_xattr(_blob, BLOB_SNAPSHOT, &_parent->id, sizeof(spdk_blob_id), true);
6952 [ - + ]: 24 : if (bserrno != 0) {
6953 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
6954 : 0 : return;
6955 : : }
6956 : :
6957 [ - + ]: 24 : assert(_parent != NULL);
6958 : :
6959 : 24 : bs_blob_list_remove(_blob);
6960 : 24 : _blob->parent_id = _parent->id;
6961 : :
6962 : 24 : blob_back_bs_destroy(_blob);
6963 : 24 : _blob->back_bs_dev = bs_create_blob_bs_dev(_parent);
6964 : 24 : bs_blob_list_add(_blob);
6965 : :
6966 : 24 : spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
6967 : : }
6968 : :
6969 : : static void
6970 : 112 : bs_inflate_blob_done(struct spdk_clone_snapshot_ctx *ctx)
6971 : : {
6972 : 112 : struct spdk_blob *_blob = ctx->original.blob;
6973 : : struct spdk_blob *_parent;
6974 : :
6975 [ + + ]: 112 : if (ctx->allocate_all) {
6976 : : /* remove thin provisioning */
6977 : 64 : bs_blob_list_remove(_blob);
6978 [ + + ]: 64 : if (_blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
6979 : 16 : blob_remove_xattr(_blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
6980 : 16 : _blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
6981 : : } else {
6982 : 48 : blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
6983 : : }
6984 : 64 : _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV;
6985 : 64 : blob_back_bs_destroy(_blob);
6986 : 64 : _blob->parent_id = SPDK_BLOBID_INVALID;
6987 : : } else {
6988 : : /* For now, esnap clones always have allocate_all set. */
6989 [ - + ]: 48 : assert(!blob_is_esnap_clone(_blob));
6990 : :
6991 : 48 : _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob;
6992 [ + + ]: 48 : if (_parent->parent_id != SPDK_BLOBID_INVALID) {
6993 : : /* We must change the parent of the inflated blob */
6994 : 24 : spdk_bs_open_blob(_blob->bs, _parent->parent_id,
6995 : : bs_inflate_blob_set_parent_cpl, ctx);
6996 : 24 : return;
6997 : : }
6998 : :
6999 : 24 : bs_blob_list_remove(_blob);
7000 : 24 : _blob->parent_id = SPDK_BLOBID_INVALID;
7001 : 24 : blob_back_bs_destroy(_blob);
7002 : 24 : _blob->back_bs_dev = bs_create_zeroes_dev();
7003 : : }
7004 : :
7005 : : /* Temporarily override md_ro flag for MD modification */
7006 : 88 : _blob->md_ro = false;
7007 : 88 : blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
7008 : 88 : _blob->state = SPDK_BLOB_STATE_DIRTY;
7009 : :
7010 : 88 : spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
7011 : : }
7012 : :
7013 : : /* Check if cluster needs allocation */
7014 : : static inline bool
7015 : 2400 : bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all)
7016 : : {
7017 : : struct spdk_blob_bs_dev *b;
7018 : :
7019 [ - + ]: 2400 : assert(blob != NULL);
7020 : :
7021 [ + + ]: 2400 : if (blob->active.clusters[cluster] != 0) {
7022 : : /* Cluster is already allocated */
7023 : 64 : return false;
7024 : : }
7025 : :
7026 [ + + ]: 2336 : if (blob->parent_id == SPDK_BLOBID_INVALID) {
7027 : : /* Blob have no parent blob */
7028 : 160 : return allocate_all;
7029 : : }
7030 : :
7031 [ + + ]: 2176 : if (blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
7032 : 128 : return true;
7033 : : }
7034 : :
7035 : 2048 : b = (struct spdk_blob_bs_dev *)blob->back_bs_dev;
7036 [ + + + + ]: 2048 : return (allocate_all || b->blob->active.clusters[cluster] != 0);
7037 : : }
7038 : :
7039 : : static void
7040 : 1016 : bs_inflate_blob_touch_next(void *cb_arg, int bserrno)
7041 : : {
7042 : 1016 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
7043 : 1016 : struct spdk_blob *_blob = ctx->original.blob;
7044 : 1016 : struct spdk_bs_cpl cpl;
7045 : : spdk_bs_user_op_t *op;
7046 : : uint64_t offset;
7047 : :
7048 [ - + ]: 1016 : if (bserrno != 0) {
7049 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
7050 : 0 : return;
7051 : : }
7052 : :
7053 [ + + ]: 1312 : for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) {
7054 [ + + ]: 1200 : if (bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) {
7055 : 904 : break;
7056 : : }
7057 : : }
7058 : :
7059 [ + + ]: 1016 : if (ctx->cluster < _blob->active.num_clusters) {
7060 : 904 : offset = bs_cluster_to_lba(_blob->bs, ctx->cluster);
7061 : :
7062 : : /* We may safely increment a cluster before copying */
7063 : 904 : ctx->cluster++;
7064 : :
7065 : : /* Use a dummy 0B read as a context for cluster copy */
7066 : 904 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
7067 : 904 : cpl.u.blob_basic.cb_fn = bs_inflate_blob_touch_next;
7068 : 904 : cpl.u.blob_basic.cb_arg = ctx;
7069 : :
7070 : 904 : op = bs_user_op_alloc(ctx->channel, &cpl, SPDK_BLOB_READ, _blob,
7071 : : NULL, 0, offset, 0);
7072 [ - + ]: 904 : if (!op) {
7073 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, -ENOMEM);
7074 : 0 : return;
7075 : : }
7076 : :
7077 : 904 : bs_allocate_and_copy_cluster(_blob, ctx->channel, offset, op);
7078 : : } else {
7079 : 112 : bs_inflate_blob_done(ctx);
7080 : : }
7081 : : }
7082 : :
7083 : : static void
7084 : 120 : bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
7085 : : {
7086 : 120 : struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
7087 : : uint64_t clusters_needed;
7088 : : uint64_t i;
7089 : :
7090 [ - + ]: 120 : if (bserrno != 0) {
7091 : 0 : bs_clone_snapshot_cleanup_finish(ctx, bserrno);
7092 : 0 : return;
7093 : : }
7094 : :
7095 : 120 : ctx->original.blob = _blob;
7096 : 120 : ctx->original.md_ro = _blob->md_ro;
7097 : :
7098 [ - + ]: 120 : if (_blob->locked_operation_in_progress) {
7099 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot inflate blob - another operation in progress\n");
7100 : 0 : ctx->bserrno = -EBUSY;
7101 : 0 : spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
7102 : 0 : return;
7103 : : }
7104 : :
7105 : 120 : _blob->locked_operation_in_progress = true;
7106 : :
7107 [ + + + ]: 120 : switch (_blob->parent_id) {
7108 : 16 : case SPDK_BLOBID_INVALID:
7109 [ + + ]: 16 : if (!ctx->allocate_all) {
7110 : : /* This blob has no parent, so we cannot decouple it. */
7111 : 8 : SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n");
7112 : 8 : bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL);
7113 : 8 : return;
7114 : : }
7115 : 8 : break;
7116 : 16 : case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
7117 : : /*
7118 : : * It would be better to rely on back_bs_dev->is_zeroes(), to determine which
7119 : : * clusters require allocation. Until there is a blobstore consumer that
7120 : : * uses esnaps with an spdk_bs_dev that implements a useful is_zeroes() it is not
7121 : : * worth the effort.
7122 : : */
7123 : 16 : ctx->allocate_all = true;
7124 : 16 : break;
7125 : 88 : default:
7126 : 88 : break;
7127 : : }
7128 : :
7129 [ - + ]: 112 : if (spdk_blob_is_thin_provisioned(_blob) == false) {
7130 : : /* This is not thin provisioned blob. No need to inflate. */
7131 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, 0);
7132 : 0 : return;
7133 : : }
7134 : :
7135 : : /* Do two passes - one to verify that we can obtain enough clusters
7136 : : * and another to actually claim them.
7137 : : */
7138 : 112 : clusters_needed = 0;
7139 [ + + ]: 1312 : for (i = 0; i < _blob->active.num_clusters; i++) {
7140 [ + + ]: 1200 : if (bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) {
7141 : 904 : clusters_needed++;
7142 : : }
7143 : : }
7144 : :
7145 [ - + ]: 112 : if (clusters_needed > _blob->bs->num_free_clusters) {
7146 : : /* Not enough free clusters. Cannot satisfy the request. */
7147 : 0 : bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC);
7148 : 0 : return;
7149 : : }
7150 : :
7151 : 112 : ctx->cluster = 0;
7152 : 112 : bs_inflate_blob_touch_next(ctx, 0);
7153 : : }
7154 : :
7155 : : static void
7156 : 120 : bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7157 : : spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg)
7158 : : {
7159 : 120 : struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
7160 : :
7161 [ - + ]: 120 : if (!ctx) {
7162 : 0 : cb_fn(cb_arg, -ENOMEM);
7163 : 0 : return;
7164 : : }
7165 : 120 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
7166 : 120 : ctx->cpl.u.bs_basic.cb_fn = cb_fn;
7167 : 120 : ctx->cpl.u.bs_basic.cb_arg = cb_arg;
7168 : 120 : ctx->bserrno = 0;
7169 : 120 : ctx->original.id = blobid;
7170 : 120 : ctx->channel = channel;
7171 : 120 : ctx->allocate_all = allocate_all;
7172 : :
7173 : 120 : spdk_bs_open_blob(bs, ctx->original.id, bs_inflate_blob_open_cpl, ctx);
7174 : : }
7175 : :
7176 : : void
7177 : 56 : spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7178 : : spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
7179 : : {
7180 : 56 : bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg);
7181 : 56 : }
7182 : :
7183 : : void
7184 : 64 : spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7185 : : spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
7186 : : {
7187 : 64 : bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg);
7188 : 64 : }
7189 : : /* END spdk_bs_inflate_blob */
7190 : :
7191 : : /* START spdk_bs_blob_shallow_copy */
7192 : :
7193 : : struct shallow_copy_ctx {
7194 : : struct spdk_bs_cpl cpl;
7195 : : int bserrno;
7196 : :
7197 : : /* Blob source for copy */
7198 : : struct spdk_blob_store *bs;
7199 : : spdk_blob_id blobid;
7200 : : struct spdk_blob *blob;
7201 : : struct spdk_io_channel *blob_channel;
7202 : :
7203 : : /* Destination device for copy */
7204 : : struct spdk_bs_dev *ext_dev;
7205 : : struct spdk_io_channel *ext_channel;
7206 : :
7207 : : /* Current cluster for copy operation */
7208 : : uint64_t cluster;
7209 : :
7210 : : /* Buffer for blob reading */
7211 : : uint8_t *read_buff;
7212 : :
7213 : : /* Struct for external device writing */
7214 : : struct spdk_bs_dev_cb_args ext_args;
7215 : :
7216 : : /* Actual number of copied clusters */
7217 : : uint64_t copied_clusters_count;
7218 : :
7219 : : /* Status callback for updates about the ongoing operation */
7220 : : spdk_blob_shallow_copy_status status_cb;
7221 : :
7222 : : /* Argument passed to function status_cb */
7223 : : void *status_cb_arg;
7224 : : };
7225 : :
7226 : : static void
7227 : 32 : bs_shallow_copy_cleanup_finish(void *cb_arg, int bserrno)
7228 : : {
7229 : 32 : struct shallow_copy_ctx *ctx = cb_arg;
7230 : 32 : struct spdk_bs_cpl *cpl = &ctx->cpl;
7231 : :
7232 [ - + ]: 32 : if (bserrno != 0) {
7233 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, cleanup error %d\n", ctx->blob->id, bserrno);
7234 : 0 : ctx->bserrno = bserrno;
7235 : : }
7236 : :
7237 : 32 : ctx->ext_dev->destroy_channel(ctx->ext_dev, ctx->ext_channel);
7238 : 32 : spdk_free(ctx->read_buff);
7239 : :
7240 : 32 : cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
7241 : :
7242 : 32 : free(ctx);
7243 : 32 : }
7244 : :
7245 : : static void
7246 : 16 : bs_shallow_copy_bdev_write_cpl(struct spdk_io_channel *channel, void *cb_arg, int bserrno)
7247 : : {
7248 : 16 : struct shallow_copy_ctx *ctx = cb_arg;
7249 : 16 : struct spdk_blob *_blob = ctx->blob;
7250 : :
7251 [ - + ]: 16 : if (bserrno != 0) {
7252 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, ext dev write error %d\n", ctx->blob->id, bserrno);
7253 : 0 : ctx->bserrno = bserrno;
7254 : 0 : _blob->locked_operation_in_progress = false;
7255 : 0 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7256 : 0 : return;
7257 : : }
7258 : :
7259 : 16 : ctx->cluster++;
7260 [ + - ]: 16 : if (ctx->status_cb) {
7261 : 16 : ctx->copied_clusters_count++;
7262 : 16 : ctx->status_cb(ctx->copied_clusters_count, ctx->status_cb_arg);
7263 : : }
7264 : :
7265 : 16 : bs_shallow_copy_cluster_find_next(ctx);
7266 : : }
7267 : :
7268 : : static void
7269 : 16 : bs_shallow_copy_blob_read_cpl(void *cb_arg, int bserrno)
7270 : : {
7271 : 16 : struct shallow_copy_ctx *ctx = cb_arg;
7272 : 16 : struct spdk_bs_dev *ext_dev = ctx->ext_dev;
7273 : 16 : struct spdk_blob *_blob = ctx->blob;
7274 : :
7275 [ - + ]: 16 : if (bserrno != 0) {
7276 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob read error %d\n", ctx->blob->id, bserrno);
7277 : 0 : ctx->bserrno = bserrno;
7278 : 0 : _blob->locked_operation_in_progress = false;
7279 : 0 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7280 : 0 : return;
7281 : : }
7282 : :
7283 : 16 : ctx->ext_args.channel = ctx->ext_channel;
7284 : 16 : ctx->ext_args.cb_fn = bs_shallow_copy_bdev_write_cpl;
7285 : 16 : ctx->ext_args.cb_arg = ctx;
7286 : :
7287 : 16 : ext_dev->write(ext_dev, ctx->ext_channel, ctx->read_buff,
7288 : 16 : bs_cluster_to_lba(_blob->bs, ctx->cluster),
7289 : 16 : bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
7290 : : &ctx->ext_args);
7291 : : }
7292 : :
7293 : : static void
7294 : 24 : bs_shallow_copy_cluster_find_next(void *cb_arg)
7295 : : {
7296 : 24 : struct shallow_copy_ctx *ctx = cb_arg;
7297 : 24 : struct spdk_blob *_blob = ctx->blob;
7298 : :
7299 [ + + ]: 40 : while (ctx->cluster < _blob->active.num_clusters) {
7300 [ + + ]: 32 : if (_blob->active.clusters[ctx->cluster] != 0) {
7301 : 16 : break;
7302 : : }
7303 : :
7304 : 16 : ctx->cluster++;
7305 : : }
7306 : :
7307 [ + + ]: 24 : if (ctx->cluster < _blob->active.num_clusters) {
7308 : 16 : blob_request_submit_op_single(ctx->blob_channel, _blob, ctx->read_buff,
7309 : 16 : bs_cluster_to_lba(_blob->bs, ctx->cluster),
7310 : 16 : bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
7311 : : bs_shallow_copy_blob_read_cpl, ctx, SPDK_BLOB_READ);
7312 : : } else {
7313 : 8 : _blob->locked_operation_in_progress = false;
7314 : 8 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7315 : : }
7316 : 24 : }
7317 : :
7318 : : static void
7319 : 32 : bs_shallow_copy_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
7320 : : {
7321 : 32 : struct shallow_copy_ctx *ctx = cb_arg;
7322 : 32 : struct spdk_bs_dev *ext_dev = ctx->ext_dev;
7323 : : uint32_t blob_block_size;
7324 : : uint64_t blob_total_size;
7325 : :
7326 [ - + ]: 32 : if (bserrno != 0) {
7327 : 0 : SPDK_ERRLOG("Shallow copy blob open error %d\n", bserrno);
7328 : 0 : ctx->bserrno = bserrno;
7329 : 0 : bs_shallow_copy_cleanup_finish(ctx, 0);
7330 : 0 : return;
7331 : : }
7332 : :
7333 [ + + ]: 32 : if (!spdk_blob_is_read_only(_blob)) {
7334 : 8 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob must be read only\n", _blob->id);
7335 : 8 : ctx->bserrno = -EPERM;
7336 : 8 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7337 : 8 : return;
7338 : : }
7339 : :
7340 : 24 : blob_block_size = _blob->bs->dev->blocklen;
7341 : 24 : blob_total_size = spdk_blob_get_num_clusters(_blob) * spdk_bs_get_cluster_size(_blob->bs);
7342 : :
7343 [ + + ]: 24 : if (blob_total_size > ext_dev->blockcnt * ext_dev->blocklen) {
7344 : 8 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device must have at least blob size\n",
7345 : : _blob->id);
7346 : 8 : ctx->bserrno = -EINVAL;
7347 : 8 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7348 : 8 : return;
7349 : : }
7350 : :
7351 [ + + ]: 16 : if (blob_block_size % ext_dev->blocklen != 0) {
7352 : 8 : SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device block size is not compatible with \
7353 : : blobstore block size\n", _blob->id);
7354 : 8 : ctx->bserrno = -EINVAL;
7355 : 8 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7356 : 8 : return;
7357 : : }
7358 : :
7359 : 8 : ctx->blob = _blob;
7360 : :
7361 [ - + ]: 8 : if (_blob->locked_operation_in_progress) {
7362 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "blob 0x%" PRIx64 " shallow copy - another operation in progress\n", _blob->id);
7363 : 0 : ctx->bserrno = -EBUSY;
7364 : 0 : spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
7365 : 0 : return;
7366 : : }
7367 : :
7368 : 8 : _blob->locked_operation_in_progress = true;
7369 : :
7370 : 8 : ctx->cluster = 0;
7371 : 8 : bs_shallow_copy_cluster_find_next(ctx);
7372 : : }
7373 : :
7374 : : int
7375 : 32 : spdk_bs_blob_shallow_copy(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
7376 : : spdk_blob_id blobid, struct spdk_bs_dev *ext_dev,
7377 : : spdk_blob_shallow_copy_status status_cb_fn, void *status_cb_arg,
7378 : : spdk_blob_op_complete cb_fn, void *cb_arg)
7379 : : {
7380 : : struct shallow_copy_ctx *ctx;
7381 : : struct spdk_io_channel *ext_channel;
7382 : :
7383 : 32 : ctx = calloc(1, sizeof(*ctx));
7384 [ - + ]: 32 : if (!ctx) {
7385 : 0 : return -ENOMEM;
7386 : : }
7387 : :
7388 : 32 : ctx->bs = bs;
7389 : 32 : ctx->blobid = blobid;
7390 : 32 : ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
7391 : 32 : ctx->cpl.u.bs_basic.cb_fn = cb_fn;
7392 : 32 : ctx->cpl.u.bs_basic.cb_arg = cb_arg;
7393 : 32 : ctx->bserrno = 0;
7394 : 32 : ctx->blob_channel = channel;
7395 : 32 : ctx->status_cb = status_cb_fn;
7396 : 32 : ctx->status_cb_arg = status_cb_arg;
7397 : 32 : ctx->read_buff = spdk_malloc(bs->cluster_sz, bs->dev->blocklen, NULL,
7398 : : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
7399 [ - + ]: 32 : if (!ctx->read_buff) {
7400 : 0 : free(ctx);
7401 : 0 : return -ENOMEM;
7402 : : }
7403 : :
7404 : 32 : ext_channel = ext_dev->create_channel(ext_dev);
7405 [ - + ]: 32 : if (!ext_channel) {
7406 : 0 : spdk_free(ctx->read_buff);
7407 : 0 : free(ctx);
7408 : 0 : return -ENOMEM;
7409 : : }
7410 : 32 : ctx->ext_dev = ext_dev;
7411 : 32 : ctx->ext_channel = ext_channel;
7412 : :
7413 : 32 : spdk_bs_open_blob(ctx->bs, ctx->blobid, bs_shallow_copy_blob_open_cpl, ctx);
7414 : :
7415 : 32 : return 0;
7416 : : }
7417 : : /* END spdk_bs_blob_shallow_copy */
7418 : :
7419 : : /* START spdk_bs_blob_set_parent */
7420 : :
7421 : : struct set_parent_ctx {
7422 : : struct spdk_blob_store *bs;
7423 : : int bserrno;
7424 : : spdk_bs_op_complete cb_fn;
7425 : : void *cb_arg;
7426 : :
7427 : : struct spdk_blob *blob;
7428 : : bool blob_md_ro;
7429 : :
7430 : : struct blob_parent parent;
7431 : : };
7432 : :
7433 : : static void
7434 : 48 : bs_set_parent_cleanup_finish(void *cb_arg, int bserrno)
7435 : : {
7436 : 48 : struct set_parent_ctx *ctx = cb_arg;
7437 : :
7438 [ - + ]: 48 : assert(ctx != NULL);
7439 : :
7440 [ - + ]: 48 : if (bserrno != 0) {
7441 : 0 : SPDK_ERRLOG("blob set parent finish error %d\n", bserrno);
7442 [ # # ]: 0 : if (ctx->bserrno == 0) {
7443 : 0 : ctx->bserrno = bserrno;
7444 : : }
7445 : : }
7446 : :
7447 : 48 : ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
7448 : :
7449 : 48 : free(ctx);
7450 : 48 : }
7451 : :
7452 : : static void
7453 : 40 : bs_set_parent_close_snapshot(void *cb_arg, int bserrno)
7454 : : {
7455 : 40 : struct set_parent_ctx *ctx = cb_arg;
7456 : :
7457 [ + + ]: 40 : if (ctx->bserrno != 0) {
7458 : 16 : spdk_blob_close(ctx->parent.u.snapshot.blob, bs_set_parent_cleanup_finish, ctx);
7459 : 16 : return;
7460 : : }
7461 : :
7462 [ - + ]: 24 : if (bserrno != 0) {
7463 : 0 : SPDK_ERRLOG("blob close error %d\n", bserrno);
7464 : 0 : ctx->bserrno = bserrno;
7465 : : }
7466 : :
7467 : 24 : bs_set_parent_cleanup_finish(ctx, ctx->bserrno);
7468 : : }
7469 : :
7470 : : static void
7471 : 24 : bs_set_parent_close_blob(void *cb_arg, int bserrno)
7472 : : {
7473 : 24 : struct set_parent_ctx *ctx = cb_arg;
7474 : 24 : struct spdk_blob *blob = ctx->blob;
7475 : 24 : struct spdk_blob *snapshot = ctx->parent.u.snapshot.blob;
7476 : :
7477 [ - + - - ]: 24 : if (bserrno != 0 && ctx->bserrno == 0) {
7478 : 0 : SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
7479 : 0 : ctx->bserrno = bserrno;
7480 : : }
7481 : :
7482 : : /* Revert md_ro to original state */
7483 : 24 : blob->md_ro = ctx->blob_md_ro;
7484 : :
7485 : 24 : blob->locked_operation_in_progress = false;
7486 : 24 : snapshot->locked_operation_in_progress = false;
7487 : :
7488 : 24 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7489 : 24 : }
7490 : :
7491 : : static void
7492 : 24 : bs_set_parent_set_back_bs_dev_done(void *cb_arg, int bserrno)
7493 : : {
7494 : 24 : struct set_parent_ctx *ctx = cb_arg;
7495 : 24 : struct spdk_blob *blob = ctx->blob;
7496 : :
7497 [ - + ]: 24 : if (bserrno != 0) {
7498 : 0 : SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
7499 : 0 : ctx->bserrno = bserrno;
7500 : 0 : bs_set_parent_close_blob(ctx, bserrno);
7501 : 0 : return;
7502 : : }
7503 : :
7504 : 24 : spdk_blob_sync_md(blob, bs_set_parent_close_blob, ctx);
7505 : : }
7506 : :
7507 : : static int
7508 : 24 : bs_set_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
7509 : : {
7510 : : int rc;
7511 : :
7512 : 24 : bs_blob_list_remove(blob);
7513 : :
7514 : 24 : rc = blob_set_xattr(blob, BLOB_SNAPSHOT, &parent->u.snapshot.id, sizeof(spdk_blob_id), true);
7515 [ - + ]: 24 : if (rc != 0) {
7516 : 0 : SPDK_ERRLOG("error %d setting snapshot xattr\n", rc);
7517 : 0 : return rc;
7518 : : }
7519 : 24 : blob->parent_id = parent->u.snapshot.id;
7520 : :
7521 [ + + ]: 24 : if (blob_is_esnap_clone(blob)) {
7522 : : /* Remove the xattr that references the external snapshot */
7523 : 8 : blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
7524 : 8 : blob_remove_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
7525 : : }
7526 : :
7527 : 24 : bs_blob_list_add(blob);
7528 : :
7529 : 24 : return 0;
7530 : : }
7531 : :
7532 : : static void
7533 : 40 : bs_set_parent_snapshot_open_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
7534 : : {
7535 : 40 : struct set_parent_ctx *ctx = cb_arg;
7536 : 40 : struct spdk_blob *blob = ctx->blob;
7537 : : struct spdk_bs_dev *back_bs_dev;
7538 : :
7539 [ - + ]: 40 : if (bserrno != 0) {
7540 : 0 : SPDK_ERRLOG("snapshot open error %d\n", bserrno);
7541 : 0 : ctx->bserrno = bserrno;
7542 : 0 : spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
7543 : 0 : return;
7544 : : }
7545 : :
7546 : 40 : ctx->parent.u.snapshot.blob = snapshot;
7547 : 40 : ctx->parent.u.snapshot.id = snapshot->id;
7548 : :
7549 [ + + ]: 40 : if (!spdk_blob_is_snapshot(snapshot)) {
7550 : 8 : SPDK_ERRLOG("parent blob is not a snapshot\n");
7551 : 8 : ctx->bserrno = -EINVAL;
7552 : 8 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7553 : 8 : return;
7554 : : }
7555 : :
7556 [ + + ]: 32 : if (blob->active.num_clusters != snapshot->active.num_clusters) {
7557 : 8 : SPDK_ERRLOG("parent blob has a number of clusters different from child's ones\n");
7558 : 8 : ctx->bserrno = -EINVAL;
7559 : 8 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7560 : 8 : return;
7561 : : }
7562 : :
7563 [ + - - + ]: 24 : if (blob->locked_operation_in_progress || snapshot->locked_operation_in_progress) {
7564 : 0 : SPDK_ERRLOG("cannot set parent of blob, another operation in progress\n");
7565 : 0 : ctx->bserrno = -EBUSY;
7566 : 0 : spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
7567 : 0 : return;
7568 : : }
7569 : :
7570 : 24 : blob->locked_operation_in_progress = true;
7571 : 24 : snapshot->locked_operation_in_progress = true;
7572 : :
7573 : : /* Temporarily override md_ro flag for MD modification */
7574 : 24 : blob->md_ro = false;
7575 : :
7576 : 24 : back_bs_dev = bs_create_blob_bs_dev(snapshot);
7577 : :
7578 : 24 : blob_set_back_bs_dev(blob, back_bs_dev, bs_set_parent_refs, &ctx->parent,
7579 : : bs_set_parent_set_back_bs_dev_done,
7580 : : ctx);
7581 : : }
7582 : :
7583 : : static void
7584 : 48 : bs_set_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
7585 : : {
7586 : 48 : struct set_parent_ctx *ctx = cb_arg;
7587 : :
7588 [ - + ]: 48 : if (bserrno != 0) {
7589 : 0 : SPDK_ERRLOG("blob open error %d\n", bserrno);
7590 : 0 : ctx->bserrno = bserrno;
7591 : 0 : bs_set_parent_cleanup_finish(ctx, 0);
7592 : 0 : return;
7593 : : }
7594 : :
7595 [ + + ]: 48 : if (!spdk_blob_is_thin_provisioned(blob)) {
7596 : 8 : SPDK_ERRLOG("blob is not thin-provisioned\n");
7597 : 8 : ctx->bserrno = -EINVAL;
7598 : 8 : spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
7599 : 8 : return;
7600 : : }
7601 : :
7602 : 40 : ctx->blob = blob;
7603 : 40 : ctx->blob_md_ro = blob->md_ro;
7604 : :
7605 : 40 : spdk_bs_open_blob(ctx->bs, ctx->parent.u.snapshot.id, bs_set_parent_snapshot_open_cpl, ctx);
7606 : : }
7607 : :
7608 : : void
7609 : 72 : spdk_bs_blob_set_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
7610 : : spdk_blob_id snapshot_id, spdk_blob_op_complete cb_fn, void *cb_arg)
7611 : : {
7612 : : struct set_parent_ctx *ctx;
7613 : :
7614 [ + + ]: 72 : if (snapshot_id == SPDK_BLOBID_INVALID) {
7615 : 8 : SPDK_ERRLOG("snapshot id not valid\n");
7616 : 8 : cb_fn(cb_arg, -EINVAL);
7617 : 8 : return;
7618 : : }
7619 : :
7620 [ + + ]: 64 : if (blob_id == snapshot_id) {
7621 : 8 : SPDK_ERRLOG("blob id and snapshot id cannot be the same\n");
7622 : 8 : cb_fn(cb_arg, -EINVAL);
7623 : 8 : return;
7624 : : }
7625 : :
7626 [ + + ]: 56 : if (spdk_blob_get_parent_snapshot(bs, blob_id) == snapshot_id) {
7627 : 8 : SPDK_NOTICELOG("snapshot is already the parent of blob\n");
7628 : 8 : cb_fn(cb_arg, -EEXIST);
7629 : 8 : return;
7630 : : }
7631 : :
7632 : 48 : ctx = calloc(1, sizeof(*ctx));
7633 [ - + ]: 48 : if (!ctx) {
7634 : 0 : cb_fn(cb_arg, -ENOMEM);
7635 : 0 : return;
7636 : : }
7637 : :
7638 : 48 : ctx->bs = bs;
7639 : 48 : ctx->parent.u.snapshot.id = snapshot_id;
7640 : 48 : ctx->cb_fn = cb_fn;
7641 : 48 : ctx->cb_arg = cb_arg;
7642 : 48 : ctx->bserrno = 0;
7643 : :
7644 : 48 : spdk_bs_open_blob(bs, blob_id, bs_set_parent_blob_open_cpl, ctx);
7645 : : }
7646 : : /* END spdk_bs_blob_set_parent */
7647 : :
7648 : : /* START spdk_bs_blob_set_external_parent */
7649 : :
7650 : : static void
7651 : 32 : bs_set_external_parent_cleanup_finish(void *cb_arg, int bserrno)
7652 : : {
7653 : 32 : struct set_parent_ctx *ctx = cb_arg;
7654 : :
7655 [ - + ]: 32 : if (bserrno != 0) {
7656 : 0 : SPDK_ERRLOG("blob set external parent finish error %d\n", bserrno);
7657 [ # # ]: 0 : if (ctx->bserrno == 0) {
7658 : 0 : ctx->bserrno = bserrno;
7659 : : }
7660 : : }
7661 : :
7662 : 32 : ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
7663 : :
7664 : 32 : free(ctx->parent.u.esnap.id);
7665 : 32 : free(ctx);
7666 : 32 : }
7667 : :
7668 : : static void
7669 : 16 : bs_set_external_parent_close_blob(void *cb_arg, int bserrno)
7670 : : {
7671 : 16 : struct set_parent_ctx *ctx = cb_arg;
7672 : 16 : struct spdk_blob *blob = ctx->blob;
7673 : :
7674 [ - + - - ]: 16 : if (bserrno != 0 && ctx->bserrno == 0) {
7675 : 0 : SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
7676 : 0 : ctx->bserrno = bserrno;
7677 : : }
7678 : :
7679 : : /* Revert md_ro to original state */
7680 : 16 : blob->md_ro = ctx->blob_md_ro;
7681 : :
7682 : 16 : blob->locked_operation_in_progress = false;
7683 : :
7684 : 16 : spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
7685 : 16 : }
7686 : :
7687 : : static void
7688 : 16 : bs_set_external_parent_unfrozen(void *cb_arg, int bserrno)
7689 : : {
7690 : 16 : struct set_parent_ctx *ctx = cb_arg;
7691 : 16 : struct spdk_blob *blob = ctx->blob;
7692 : :
7693 [ - + ]: 16 : if (bserrno != 0) {
7694 : 0 : SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
7695 : 0 : ctx->bserrno = bserrno;
7696 : 0 : bs_set_external_parent_close_blob(ctx, bserrno);
7697 : 0 : return;
7698 : : }
7699 : :
7700 : 16 : spdk_blob_sync_md(blob, bs_set_external_parent_close_blob, ctx);
7701 : : }
7702 : :
7703 : : static int
7704 : 16 : bs_set_external_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
7705 : : {
7706 : : int rc;
7707 : :
7708 : 16 : bs_blob_list_remove(blob);
7709 : :
7710 [ - + ]: 16 : if (spdk_blob_is_clone(blob)) {
7711 : : /* Remove the xattr that references the snapshot */
7712 : 0 : blob->parent_id = SPDK_BLOBID_INVALID;
7713 : 0 : blob_remove_xattr(blob, BLOB_SNAPSHOT, true);
7714 : : }
7715 : :
7716 : 16 : rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, parent->u.esnap.id,
7717 : 16 : parent->u.esnap.id_len, true);
7718 [ - + ]: 16 : if (rc != 0) {
7719 : 0 : SPDK_ERRLOG("error %d setting external snapshot xattr\n", rc);
7720 : 0 : return rc;
7721 : : }
7722 : 16 : blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
7723 : :
7724 : 16 : bs_blob_list_add(blob);
7725 : :
7726 : 16 : return 0;
7727 : : }
7728 : :
7729 : : static void
7730 : 32 : bs_set_external_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
7731 : : {
7732 : 32 : struct set_parent_ctx *ctx = cb_arg;
7733 : 32 : const void *esnap_id;
7734 : 32 : size_t esnap_id_len;
7735 : : int rc;
7736 : :
7737 [ - + ]: 32 : if (bserrno != 0) {
7738 : 0 : SPDK_ERRLOG("blob open error %d\n", bserrno);
7739 : 0 : ctx->bserrno = bserrno;
7740 : 0 : bs_set_parent_cleanup_finish(ctx, 0);
7741 : 0 : return;
7742 : : }
7743 : :
7744 : 32 : ctx->blob = blob;
7745 : 32 : ctx->blob_md_ro = blob->md_ro;
7746 : :
7747 : 32 : rc = spdk_blob_get_esnap_id(blob, &esnap_id, &esnap_id_len);
7748 [ + + + - : 32 : if (rc == 0 && esnap_id != NULL && esnap_id_len == ctx->parent.u.esnap.id_len &&
+ - ]
7749 [ + - ]: 8 : memcmp(esnap_id, ctx->parent.u.esnap.id, esnap_id_len) == 0) {
7750 : 8 : SPDK_ERRLOG("external snapshot is already the parent of blob\n");
7751 : 8 : ctx->bserrno = -EEXIST;
7752 : 8 : goto error;
7753 : : }
7754 : :
7755 [ + + ]: 24 : if (!spdk_blob_is_thin_provisioned(blob)) {
7756 : 8 : SPDK_ERRLOG("blob is not thin-provisioned\n");
7757 : 8 : ctx->bserrno = -EINVAL;
7758 : 8 : goto error;
7759 : : }
7760 : :
7761 [ - + ]: 16 : if (blob->locked_operation_in_progress) {
7762 : 0 : SPDK_ERRLOG("cannot set external parent of blob, another operation in progress\n");
7763 : 0 : ctx->bserrno = -EBUSY;
7764 : 0 : goto error;
7765 : : }
7766 : :
7767 : 16 : blob->locked_operation_in_progress = true;
7768 : :
7769 : : /* Temporarily override md_ro flag for MD modification */
7770 : 16 : blob->md_ro = false;
7771 : :
7772 : 16 : blob_set_back_bs_dev(blob, ctx->parent.u.esnap.back_bs_dev, bs_set_external_parent_refs,
7773 : : &ctx->parent, bs_set_external_parent_unfrozen, ctx);
7774 : 16 : return;
7775 : :
7776 : 16 : error:
7777 : 16 : spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
7778 : : }
7779 : :
7780 : : void
7781 : 48 : spdk_bs_blob_set_external_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
7782 : : struct spdk_bs_dev *esnap_bs_dev, const void *esnap_id,
7783 : : uint32_t esnap_id_len, spdk_blob_op_complete cb_fn, void *cb_arg)
7784 : : {
7785 : : struct set_parent_ctx *ctx;
7786 : : uint64_t esnap_dev_size, cluster_sz;
7787 : :
7788 [ + + + - ]: 48 : if (sizeof(blob_id) == esnap_id_len && memcmp(&blob_id, esnap_id, sizeof(blob_id)) == 0) {
7789 : 8 : SPDK_ERRLOG("blob id and external snapshot id cannot be the same\n");
7790 : 8 : cb_fn(cb_arg, -EINVAL);
7791 : 8 : return;
7792 : : }
7793 : :
7794 : 40 : esnap_dev_size = esnap_bs_dev->blockcnt * esnap_bs_dev->blocklen;
7795 : 40 : cluster_sz = spdk_bs_get_cluster_size(bs);
7796 [ + + ]: 40 : if ((esnap_dev_size % cluster_sz) != 0) {
7797 : 8 : SPDK_ERRLOG("Esnap device size %" PRIu64 " is not an integer multiple of "
7798 : : "cluster size %" PRIu64 "\n", esnap_dev_size, cluster_sz);
7799 : 8 : cb_fn(cb_arg, -EINVAL);
7800 : 8 : return;
7801 : : }
7802 : :
7803 : 32 : ctx = calloc(1, sizeof(*ctx));
7804 [ - + ]: 32 : if (!ctx) {
7805 : 0 : cb_fn(cb_arg, -ENOMEM);
7806 : 0 : return;
7807 : : }
7808 : :
7809 : 32 : ctx->parent.u.esnap.id = calloc(1, esnap_id_len);
7810 [ - + ]: 32 : if (!ctx->parent.u.esnap.id) {
7811 : 0 : free(ctx);
7812 : 0 : cb_fn(cb_arg, -ENOMEM);
7813 : 0 : return;
7814 : : }
7815 : :
7816 : 32 : ctx->bs = bs;
7817 : 32 : ctx->parent.u.esnap.back_bs_dev = esnap_bs_dev;
7818 : 32 : memcpy(ctx->parent.u.esnap.id, esnap_id, esnap_id_len);
7819 : 32 : ctx->parent.u.esnap.id_len = esnap_id_len;
7820 : 32 : ctx->cb_fn = cb_fn;
7821 : 32 : ctx->cb_arg = cb_arg;
7822 : 32 : ctx->bserrno = 0;
7823 : :
7824 : 32 : spdk_bs_open_blob(bs, blob_id, bs_set_external_parent_blob_open_cpl, ctx);
7825 : : }
7826 : : /* END spdk_bs_blob_set_external_parent */
7827 : :
7828 : : /* START spdk_blob_resize */
7829 : : struct spdk_bs_resize_ctx {
7830 : : spdk_blob_op_complete cb_fn;
7831 : : void *cb_arg;
7832 : : struct spdk_blob *blob;
7833 : : uint64_t sz;
7834 : : int rc;
7835 : : };
7836 : :
7837 : : static void
7838 : 404 : bs_resize_unfreeze_cpl(void *cb_arg, int rc)
7839 : : {
7840 : 404 : struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
7841 : :
7842 [ - + ]: 404 : if (rc != 0) {
7843 : 0 : SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc);
7844 : : }
7845 : :
7846 [ + + ]: 404 : if (ctx->rc != 0) {
7847 : 8 : SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc);
7848 : 8 : rc = ctx->rc;
7849 : : }
7850 : :
7851 : 404 : ctx->blob->locked_operation_in_progress = false;
7852 : :
7853 : 404 : ctx->cb_fn(ctx->cb_arg, rc);
7854 : 404 : free(ctx);
7855 : 404 : }
7856 : :
7857 : : static void
7858 : 404 : bs_resize_freeze_cpl(void *cb_arg, int rc)
7859 : : {
7860 : 404 : struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
7861 : :
7862 [ - + ]: 404 : if (rc != 0) {
7863 : 0 : ctx->blob->locked_operation_in_progress = false;
7864 : 0 : ctx->cb_fn(ctx->cb_arg, rc);
7865 : 0 : free(ctx);
7866 : 0 : return;
7867 : : }
7868 : :
7869 : 404 : ctx->rc = blob_resize(ctx->blob, ctx->sz);
7870 : :
7871 : 404 : blob_unfreeze_io(ctx->blob, bs_resize_unfreeze_cpl, ctx);
7872 : : }
7873 : :
7874 : : void
7875 : 432 : spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg)
7876 : : {
7877 : : struct spdk_bs_resize_ctx *ctx;
7878 : :
7879 : 432 : blob_verify_md_op(blob);
7880 : :
7881 [ - + ]: 432 : SPDK_DEBUGLOG(blob, "Resizing blob 0x%" PRIx64 " to %" PRIu64 " clusters\n", blob->id, sz);
7882 : :
7883 [ + + ]: 432 : if (blob->md_ro) {
7884 : 8 : cb_fn(cb_arg, -EPERM);
7885 : 8 : return;
7886 : : }
7887 : :
7888 [ + + ]: 424 : if (sz == blob->active.num_clusters) {
7889 : 20 : cb_fn(cb_arg, 0);
7890 : 20 : return;
7891 : : }
7892 : :
7893 [ - + ]: 404 : if (blob->locked_operation_in_progress) {
7894 : 0 : cb_fn(cb_arg, -EBUSY);
7895 : 0 : return;
7896 : : }
7897 : :
7898 : 404 : ctx = calloc(1, sizeof(*ctx));
7899 [ - + ]: 404 : if (!ctx) {
7900 : 0 : cb_fn(cb_arg, -ENOMEM);
7901 : 0 : return;
7902 : : }
7903 : :
7904 : 404 : blob->locked_operation_in_progress = true;
7905 : 404 : ctx->cb_fn = cb_fn;
7906 : 404 : ctx->cb_arg = cb_arg;
7907 : 404 : ctx->blob = blob;
7908 : 404 : ctx->sz = sz;
7909 : 404 : blob_freeze_io(blob, bs_resize_freeze_cpl, ctx);
7910 : : }
7911 : :
7912 : : /* END spdk_blob_resize */
7913 : :
7914 : :
7915 : : /* START spdk_bs_delete_blob */
7916 : :
7917 : : static void
7918 : 2986 : bs_delete_close_cpl(void *cb_arg, int bserrno)
7919 : : {
7920 : 2986 : spdk_bs_sequence_t *seq = cb_arg;
7921 : :
7922 : 2986 : bs_sequence_finish(seq, bserrno);
7923 : 2986 : }
7924 : :
7925 : : static void
7926 : 2986 : bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
7927 : : {
7928 : 2986 : struct spdk_blob *blob = cb_arg;
7929 : :
7930 [ - + ]: 2986 : if (bserrno != 0) {
7931 : : /*
7932 : : * We already removed this blob from the blobstore tailq, so
7933 : : * we need to free it here since this is the last reference
7934 : : * to it.
7935 : : */
7936 : 0 : blob_free(blob);
7937 : 0 : bs_delete_close_cpl(seq, bserrno);
7938 : 0 : return;
7939 : : }
7940 : :
7941 : : /*
7942 : : * This will immediately decrement the ref_count and call
7943 : : * the completion routine since the metadata state is clean.
7944 : : * By calling spdk_blob_close, we reduce the number of call
7945 : : * points into code that touches the blob->open_ref count
7946 : : * and the blobstore's blob list.
7947 : : */
7948 : 2986 : spdk_blob_close(blob, bs_delete_close_cpl, seq);
7949 : : }
7950 : :
7951 : : struct delete_snapshot_ctx {
7952 : : struct spdk_blob_list *parent_snapshot_entry;
7953 : : struct spdk_blob *snapshot;
7954 : : struct spdk_blob_md_page *page;
7955 : : bool snapshot_md_ro;
7956 : : struct spdk_blob *clone;
7957 : : bool clone_md_ro;
7958 : : spdk_blob_op_with_handle_complete cb_fn;
7959 : : void *cb_arg;
7960 : : int bserrno;
7961 : : uint32_t next_extent_page;
7962 : : };
7963 : :
7964 : : static void
7965 : 220 : delete_blob_cleanup_finish(void *cb_arg, int bserrno)
7966 : : {
7967 : 220 : struct delete_snapshot_ctx *ctx = cb_arg;
7968 : :
7969 [ - + ]: 220 : if (bserrno != 0) {
7970 : 0 : SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno);
7971 : : }
7972 : :
7973 [ - + ]: 220 : assert(ctx != NULL);
7974 : :
7975 [ - + - - ]: 220 : if (bserrno != 0 && ctx->bserrno == 0) {
7976 : 0 : ctx->bserrno = bserrno;
7977 : : }
7978 : :
7979 : 220 : ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno);
7980 : 220 : spdk_free(ctx->page);
7981 : 220 : free(ctx);
7982 : 220 : }
7983 : :
7984 : : static void
7985 : 44 : delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno)
7986 : : {
7987 : 44 : struct delete_snapshot_ctx *ctx = cb_arg;
7988 : :
7989 [ - + ]: 44 : if (bserrno != 0) {
7990 : 0 : ctx->bserrno = bserrno;
7991 : 0 : SPDK_ERRLOG("Clone cleanup error %d\n", bserrno);
7992 : : }
7993 : :
7994 [ + - ]: 44 : if (ctx->bserrno != 0) {
7995 [ - + ]: 44 : assert(blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL);
7996 : 44 : RB_INSERT(spdk_blob_tree, &ctx->snapshot->bs->open_blobs, ctx->snapshot);
7997 : 44 : spdk_bit_array_set(ctx->snapshot->bs->open_blobids, ctx->snapshot->id);
7998 : : }
7999 : :
8000 : 44 : ctx->snapshot->locked_operation_in_progress = false;
8001 : 44 : ctx->snapshot->md_ro = ctx->snapshot_md_ro;
8002 : :
8003 : 44 : spdk_blob_close(ctx->snapshot, delete_blob_cleanup_finish, ctx);
8004 : 44 : }
8005 : :
8006 : : static void
8007 : 24 : delete_snapshot_cleanup_clone(void *cb_arg, int bserrno)
8008 : : {
8009 : 24 : struct delete_snapshot_ctx *ctx = cb_arg;
8010 : :
8011 : 24 : ctx->clone->locked_operation_in_progress = false;
8012 : 24 : ctx->clone->md_ro = ctx->clone_md_ro;
8013 : :
8014 : 24 : spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
8015 : 24 : }
8016 : :
8017 : : static void
8018 : 96 : delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
8019 : : {
8020 : 96 : struct delete_snapshot_ctx *ctx = cb_arg;
8021 : :
8022 [ - + ]: 96 : if (bserrno) {
8023 : 0 : ctx->bserrno = bserrno;
8024 : 0 : delete_snapshot_cleanup_clone(ctx, 0);
8025 : 0 : return;
8026 : : }
8027 : :
8028 : 96 : ctx->clone->locked_operation_in_progress = false;
8029 : 96 : spdk_blob_close(ctx->clone, delete_blob_cleanup_finish, ctx);
8030 : : }
8031 : :
8032 : : static void
8033 : 104 : delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno)
8034 : : {
8035 : 104 : struct delete_snapshot_ctx *ctx = cb_arg;
8036 : 104 : struct spdk_blob_list *parent_snapshot_entry = NULL;
8037 : 104 : struct spdk_blob_list *snapshot_entry = NULL;
8038 : 104 : struct spdk_blob_list *clone_entry = NULL;
8039 : 104 : struct spdk_blob_list *snapshot_clone_entry = NULL;
8040 : :
8041 [ + + ]: 104 : if (bserrno) {
8042 : 8 : SPDK_ERRLOG("Failed to sync MD on blob\n");
8043 : 8 : ctx->bserrno = bserrno;
8044 : 8 : delete_snapshot_cleanup_clone(ctx, 0);
8045 : 8 : return;
8046 : : }
8047 : :
8048 : : /* Get snapshot entry for the snapshot we want to remove */
8049 : 96 : snapshot_entry = bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id);
8050 : :
8051 [ - + ]: 96 : assert(snapshot_entry != NULL);
8052 : :
8053 : : /* Remove clone entry in this snapshot (at this point there can be only one clone) */
8054 : 96 : clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
8055 [ - + ]: 96 : assert(clone_entry != NULL);
8056 [ - + ]: 96 : TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
8057 : 96 : snapshot_entry->clone_count--;
8058 [ - + ]: 96 : assert(TAILQ_EMPTY(&snapshot_entry->clones));
8059 : :
8060 [ + + ]: 96 : switch (ctx->snapshot->parent_id) {
8061 : 80 : case SPDK_BLOBID_INVALID:
8062 : : case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
8063 : : /* No parent snapshot - just remove clone entry */
8064 : 80 : free(clone_entry);
8065 : 80 : break;
8066 : 16 : default:
8067 : : /* This snapshot is at the same time a clone of another snapshot - we need to
8068 : : * update parent snapshot (remove current clone, add new one inherited from
8069 : : * the snapshot that is being removed) */
8070 : :
8071 : : /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
8072 : : * snapshot that we are removing */
8073 : 16 : blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry,
8074 : : &snapshot_clone_entry);
8075 : :
8076 : : /* Switch clone entry in parent snapshot */
8077 : 16 : TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link);
8078 [ + - ]: 16 : TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link);
8079 : 16 : free(snapshot_clone_entry);
8080 : : }
8081 : :
8082 : : /* Restore md_ro flags */
8083 : 96 : ctx->clone->md_ro = ctx->clone_md_ro;
8084 : 96 : ctx->snapshot->md_ro = ctx->snapshot_md_ro;
8085 : :
8086 : 96 : blob_unfreeze_io(ctx->clone, delete_snapshot_unfreeze_cpl, ctx);
8087 : : }
8088 : :
8089 : : static void
8090 : 112 : delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno)
8091 : : {
8092 : 112 : struct delete_snapshot_ctx *ctx = cb_arg;
8093 : : uint64_t i;
8094 : :
8095 : 112 : ctx->snapshot->md_ro = false;
8096 : :
8097 [ + + ]: 112 : if (bserrno) {
8098 : 8 : SPDK_ERRLOG("Failed to sync MD on clone\n");
8099 : 8 : ctx->bserrno = bserrno;
8100 : :
8101 : : /* Restore snapshot to previous state */
8102 : 8 : bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
8103 [ - + ]: 8 : if (bserrno != 0) {
8104 : 0 : delete_snapshot_cleanup_clone(ctx, bserrno);
8105 : 0 : return;
8106 : : }
8107 : :
8108 : 8 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
8109 : 8 : return;
8110 : : }
8111 : :
8112 : : /* Clear cluster map entries for snapshot */
8113 [ + + + - ]: 1104 : for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
8114 [ + + ]: 1000 : if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) {
8115 [ + + ]: 984 : if (ctx->snapshot->active.clusters[i] != 0) {
8116 : 656 : ctx->snapshot->active.num_allocated_clusters--;
8117 : : }
8118 : 984 : ctx->snapshot->active.clusters[i] = 0;
8119 : : }
8120 : : }
8121 [ + + ]: 156 : for (i = 0; i < ctx->snapshot->active.num_extent_pages &&
8122 [ + - ]: 104 : i < ctx->clone->active.num_extent_pages; i++) {
8123 [ + + ]: 52 : if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) {
8124 : 48 : ctx->snapshot->active.extent_pages[i] = 0;
8125 : : }
8126 : : }
8127 : :
8128 : 104 : blob_set_thin_provision(ctx->snapshot);
8129 : 104 : ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY;
8130 : :
8131 [ + + ]: 104 : if (ctx->parent_snapshot_entry != NULL) {
8132 : 16 : ctx->snapshot->back_bs_dev = NULL;
8133 : : }
8134 : :
8135 : 104 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_cpl, ctx);
8136 : : }
8137 : :
8138 : : static void
8139 : 112 : delete_snapshot_update_extent_pages_cpl(struct delete_snapshot_ctx *ctx)
8140 : : {
8141 : : int bserrno;
8142 : :
8143 : : /* Delete old backing bs_dev from clone (related to snapshot that will be removed) */
8144 : 112 : blob_back_bs_destroy(ctx->clone);
8145 : :
8146 : : /* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */
8147 [ + + ]: 112 : if (ctx->snapshot->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
8148 : 16 : bserrno = bs_snapshot_copy_xattr(ctx->clone, ctx->snapshot,
8149 : : BLOB_EXTERNAL_SNAPSHOT_ID);
8150 [ - + ]: 16 : if (bserrno != 0) {
8151 : 0 : ctx->bserrno = bserrno;
8152 : :
8153 : : /* Restore snapshot to previous state */
8154 : 0 : bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
8155 [ # # ]: 0 : if (bserrno != 0) {
8156 : 0 : delete_snapshot_cleanup_clone(ctx, bserrno);
8157 : 0 : return;
8158 : : }
8159 : :
8160 : 0 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
8161 : 0 : return;
8162 : : }
8163 : 16 : ctx->clone->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
8164 : 16 : ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
8165 : : /* Do not delete the external snapshot along with this snapshot */
8166 : 16 : ctx->snapshot->back_bs_dev = NULL;
8167 : 16 : ctx->clone->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
8168 [ + + ]: 96 : } else if (ctx->parent_snapshot_entry != NULL) {
8169 : : /* ...to parent snapshot */
8170 : 16 : ctx->clone->parent_id = ctx->parent_snapshot_entry->id;
8171 : 16 : ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
8172 : 16 : blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id,
8173 : : sizeof(spdk_blob_id),
8174 : : true);
8175 : : } else {
8176 : : /* ...to blobid invalid and zeroes dev */
8177 : 80 : ctx->clone->parent_id = SPDK_BLOBID_INVALID;
8178 : 80 : ctx->clone->back_bs_dev = bs_create_zeroes_dev();
8179 : 80 : blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true);
8180 : : }
8181 : :
8182 : 112 : spdk_blob_sync_md(ctx->clone, delete_snapshot_sync_clone_cpl, ctx);
8183 : : }
8184 : :
8185 : : static void
8186 : 116 : delete_snapshot_update_extent_pages(void *cb_arg, int bserrno)
8187 : : {
8188 : 116 : struct delete_snapshot_ctx *ctx = cb_arg;
8189 : : uint32_t *extent_page;
8190 : : uint64_t i;
8191 : :
8192 [ + + ]: 168 : for (i = ctx->next_extent_page; i < ctx->snapshot->active.num_extent_pages &&
8193 [ + - ]: 108 : i < ctx->clone->active.num_extent_pages; i++) {
8194 [ + + ]: 56 : if (ctx->snapshot->active.extent_pages[i] == 0) {
8195 : : /* No extent page to use from snapshot */
8196 : 16 : continue;
8197 : : }
8198 : :
8199 : 40 : extent_page = &ctx->clone->active.extent_pages[i];
8200 [ + + ]: 40 : if (*extent_page == 0) {
8201 : : /* Copy extent page from snapshot when clone did not have a matching one */
8202 : 36 : *extent_page = ctx->snapshot->active.extent_pages[i];
8203 : 36 : continue;
8204 : : }
8205 : :
8206 : : /* Clone and snapshot both contain partially filled matching extent pages.
8207 : : * Update the clone extent page in place with cluster map containing the mix of both. */
8208 : 4 : ctx->next_extent_page = i + 1;
8209 : 4 : memset(ctx->page, 0, SPDK_BS_PAGE_SIZE);
8210 : :
8211 : 4 : blob_write_extent_page(ctx->clone, *extent_page, i * SPDK_EXTENTS_PER_EP, ctx->page,
8212 : : delete_snapshot_update_extent_pages, ctx);
8213 : 4 : return;
8214 : : }
8215 : 112 : delete_snapshot_update_extent_pages_cpl(ctx);
8216 : : }
8217 : :
8218 : : static void
8219 : 120 : delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno)
8220 : : {
8221 : 120 : struct delete_snapshot_ctx *ctx = cb_arg;
8222 : : uint64_t i;
8223 : :
8224 : : /* Temporarily override md_ro flag for clone for MD modification */
8225 : 120 : ctx->clone_md_ro = ctx->clone->md_ro;
8226 : 120 : ctx->clone->md_ro = false;
8227 : :
8228 [ + + ]: 120 : if (bserrno) {
8229 : 8 : SPDK_ERRLOG("Failed to sync MD with xattr on blob\n");
8230 : 8 : ctx->bserrno = bserrno;
8231 : 8 : delete_snapshot_cleanup_clone(ctx, 0);
8232 : 8 : return;
8233 : : }
8234 : :
8235 : : /* Copy snapshot map to clone map (only unallocated clusters in clone) */
8236 [ + + + - ]: 1192 : for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
8237 [ + + ]: 1080 : if (ctx->clone->active.clusters[i] == 0) {
8238 : 1064 : ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i];
8239 [ + + ]: 1064 : if (ctx->clone->active.clusters[i] != 0) {
8240 : 736 : ctx->clone->active.num_allocated_clusters++;
8241 : : }
8242 : : }
8243 : : }
8244 : 112 : ctx->next_extent_page = 0;
8245 : 112 : delete_snapshot_update_extent_pages(ctx, 0);
8246 : : }
8247 : :
8248 : : static void
8249 : 16 : delete_snapshot_esnap_channels_destroyed_cb(void *cb_arg, struct spdk_blob *blob, int bserrno)
8250 : : {
8251 : 16 : struct delete_snapshot_ctx *ctx = cb_arg;
8252 : :
8253 [ - + ]: 16 : if (bserrno != 0) {
8254 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to destroy esnap channels: %d\n",
8255 : : blob->id, bserrno);
8256 : : /* That error should not stop us from syncing metadata. */
8257 : : }
8258 : :
8259 : 16 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
8260 : 16 : }
8261 : :
8262 : : static void
8263 : 120 : delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno)
8264 : : {
8265 : 120 : struct delete_snapshot_ctx *ctx = cb_arg;
8266 : :
8267 [ - + ]: 120 : if (bserrno) {
8268 : 0 : SPDK_ERRLOG("Failed to freeze I/O on clone\n");
8269 : 0 : ctx->bserrno = bserrno;
8270 : 0 : delete_snapshot_cleanup_clone(ctx, 0);
8271 : 0 : return;
8272 : : }
8273 : :
8274 : : /* Temporarily override md_ro flag for snapshot for MD modification */
8275 : 120 : ctx->snapshot_md_ro = ctx->snapshot->md_ro;
8276 : 120 : ctx->snapshot->md_ro = false;
8277 : :
8278 : : /* Mark blob as pending for removal for power failure safety, use clone id for recovery */
8279 : 120 : ctx->bserrno = blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id,
8280 : : sizeof(spdk_blob_id), true);
8281 [ - + ]: 120 : if (ctx->bserrno != 0) {
8282 : 0 : delete_snapshot_cleanup_clone(ctx, 0);
8283 : 0 : return;
8284 : : }
8285 : :
8286 [ + + ]: 120 : if (blob_is_esnap_clone(ctx->snapshot)) {
8287 : 16 : blob_esnap_destroy_bs_dev_channels(ctx->snapshot, false,
8288 : : delete_snapshot_esnap_channels_destroyed_cb,
8289 : : ctx);
8290 : 16 : return;
8291 : : }
8292 : :
8293 : 104 : spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
8294 : : }
8295 : :
8296 : : static void
8297 : 140 : delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno)
8298 : : {
8299 : 140 : struct delete_snapshot_ctx *ctx = cb_arg;
8300 : :
8301 [ + + ]: 140 : if (bserrno) {
8302 : 20 : SPDK_ERRLOG("Failed to open clone\n");
8303 : 20 : ctx->bserrno = bserrno;
8304 : 20 : delete_snapshot_cleanup_snapshot(ctx, 0);
8305 : 20 : return;
8306 : : }
8307 : :
8308 : 120 : ctx->clone = clone;
8309 : :
8310 [ - + ]: 120 : if (clone->locked_operation_in_progress) {
8311 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress on its clone\n");
8312 : 0 : ctx->bserrno = -EBUSY;
8313 : 0 : spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
8314 : 0 : return;
8315 : : }
8316 : :
8317 : 120 : clone->locked_operation_in_progress = true;
8318 : :
8319 : 120 : blob_freeze_io(clone, delete_snapshot_freeze_io_cb, ctx);
8320 : : }
8321 : :
8322 : : static void
8323 : 140 : update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx)
8324 : : {
8325 : 140 : struct spdk_blob_list *snapshot_entry = NULL;
8326 : 140 : struct spdk_blob_list *clone_entry = NULL;
8327 : 140 : struct spdk_blob_list *snapshot_clone_entry = NULL;
8328 : :
8329 : : /* Get snapshot entry for the snapshot we want to remove */
8330 : 140 : snapshot_entry = bs_get_snapshot_entry(snapshot->bs, snapshot->id);
8331 : :
8332 [ - + ]: 140 : assert(snapshot_entry != NULL);
8333 : :
8334 : : /* Get clone of the snapshot (at this point there can be only one clone) */
8335 : 140 : clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
8336 [ - + ]: 140 : assert(snapshot_entry->clone_count == 1);
8337 [ - + ]: 140 : assert(clone_entry != NULL);
8338 : :
8339 : : /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
8340 : : * snapshot that we are removing */
8341 : 140 : blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry,
8342 : : &snapshot_clone_entry);
8343 : :
8344 : 140 : spdk_bs_open_blob(snapshot->bs, clone_entry->id, delete_snapshot_open_clone_cb, ctx);
8345 : 140 : }
8346 : :
8347 : : static void
8348 : 3110 : bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno)
8349 : : {
8350 : 3110 : spdk_bs_sequence_t *seq = cb_arg;
8351 : 3110 : struct spdk_blob_list *snapshot_entry = NULL;
8352 : : uint32_t page_num;
8353 : :
8354 [ + + ]: 3110 : if (bserrno) {
8355 : 124 : SPDK_ERRLOG("Failed to remove blob\n");
8356 : 124 : bs_sequence_finish(seq, bserrno);
8357 : 124 : return;
8358 : : }
8359 : :
8360 : : /* Remove snapshot from the list */
8361 : 2986 : snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
8362 [ + + ]: 2986 : if (snapshot_entry != NULL) {
8363 [ + + ]: 282 : TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link);
8364 : 282 : free(snapshot_entry);
8365 : : }
8366 : :
8367 : 2986 : page_num = bs_blobid_to_page(blob->id);
8368 : 2986 : spdk_bit_array_clear(blob->bs->used_blobids, page_num);
8369 : 2986 : blob->state = SPDK_BLOB_STATE_DIRTY;
8370 : 2986 : blob->active.num_pages = 0;
8371 : 2986 : blob_resize(blob, 0);
8372 : :
8373 : 2986 : blob_persist(seq, blob, bs_delete_persist_cpl, blob);
8374 : : }
8375 : :
8376 : : static int
8377 : 3110 : bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone)
8378 : : {
8379 : 3110 : struct spdk_blob_list *snapshot_entry = NULL;
8380 : 3110 : struct spdk_blob_list *clone_entry = NULL;
8381 : 3110 : struct spdk_blob *clone = NULL;
8382 : 3110 : bool has_one_clone = false;
8383 : :
8384 : : /* Check if this is a snapshot with clones */
8385 : 3110 : snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
8386 [ + + ]: 3110 : if (snapshot_entry != NULL) {
8387 [ + + ]: 382 : if (snapshot_entry->clone_count > 1) {
8388 : 48 : SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n");
8389 : 48 : return -EBUSY;
8390 [ + + ]: 334 : } else if (snapshot_entry->clone_count == 1) {
8391 : 140 : has_one_clone = true;
8392 : : }
8393 : : }
8394 : :
8395 : : /* Check if someone has this blob open (besides this delete context):
8396 : : * - open_ref = 1 - only this context opened blob, so it is ok to remove it
8397 : : * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot
8398 : : * and that is ok, because we will update it accordingly */
8399 [ + + + + ]: 3062 : if (blob->open_ref <= 2 && has_one_clone) {
8400 : 140 : clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
8401 [ - + ]: 140 : assert(clone_entry != NULL);
8402 : 140 : clone = blob_lookup(blob->bs, clone_entry->id);
8403 : :
8404 [ + + - + ]: 140 : if (blob->open_ref == 2 && clone == NULL) {
8405 : : /* Clone is closed and someone else opened this blob */
8406 : 0 : SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
8407 : 0 : return -EBUSY;
8408 : : }
8409 : :
8410 : 140 : *update_clone = true;
8411 : 140 : return 0;
8412 : : }
8413 : :
8414 [ + + ]: 2922 : if (blob->open_ref > 1) {
8415 : 32 : SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
8416 : 32 : return -EBUSY;
8417 : : }
8418 : :
8419 [ - + ]: 2890 : assert(has_one_clone == false);
8420 : 2890 : *update_clone = false;
8421 : 2890 : return 0;
8422 : : }
8423 : :
8424 : : static void
8425 : 0 : bs_delete_enomem_close_cpl(void *cb_arg, int bserrno)
8426 : : {
8427 : 0 : spdk_bs_sequence_t *seq = cb_arg;
8428 : :
8429 : 0 : bs_sequence_finish(seq, -ENOMEM);
8430 : 0 : }
8431 : :
8432 : : static void
8433 : 3130 : bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
8434 : : {
8435 : 3130 : spdk_bs_sequence_t *seq = cb_arg;
8436 : : struct delete_snapshot_ctx *ctx;
8437 : 3130 : bool update_clone = false;
8438 : :
8439 [ + + ]: 3130 : if (bserrno != 0) {
8440 : 20 : bs_sequence_finish(seq, bserrno);
8441 : 20 : return;
8442 : : }
8443 : :
8444 : 3110 : blob_verify_md_op(blob);
8445 : :
8446 : 3110 : ctx = calloc(1, sizeof(*ctx));
8447 [ - + ]: 3110 : if (ctx == NULL) {
8448 : 0 : spdk_blob_close(blob, bs_delete_enomem_close_cpl, seq);
8449 : 0 : return;
8450 : : }
8451 : :
8452 : 3110 : ctx->snapshot = blob;
8453 : 3110 : ctx->cb_fn = bs_delete_blob_finish;
8454 : 3110 : ctx->cb_arg = seq;
8455 : :
8456 : : /* Check if blob can be removed and if it is a snapshot with clone on top of it */
8457 : 3110 : ctx->bserrno = bs_is_blob_deletable(blob, &update_clone);
8458 [ + + ]: 3110 : if (ctx->bserrno) {
8459 : 80 : spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
8460 : 80 : return;
8461 : : }
8462 : :
8463 [ - + ]: 3030 : if (blob->locked_operation_in_progress) {
8464 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress\n");
8465 : 0 : ctx->bserrno = -EBUSY;
8466 : 0 : spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
8467 : 0 : return;
8468 : : }
8469 : :
8470 : 3030 : blob->locked_operation_in_progress = true;
8471 : :
8472 : : /*
8473 : : * Remove the blob from the blob_store list now, to ensure it does not
8474 : : * get returned after this point by blob_lookup().
8475 : : */
8476 : 3030 : spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
8477 : 3030 : RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
8478 : :
8479 [ + + ]: 3030 : if (update_clone) {
8480 : 140 : ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
8481 [ - + ]: 140 : if (!ctx->page) {
8482 : 0 : ctx->bserrno = -ENOMEM;
8483 : 0 : spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
8484 : 0 : return;
8485 : : }
8486 : : /* This blob is a snapshot with active clone - update clone first */
8487 : 140 : update_clone_on_snapshot_deletion(blob, ctx);
8488 : : } else {
8489 : : /* This blob does not have any clones - just remove it */
8490 : 2890 : bs_blob_list_remove(blob);
8491 : 2890 : bs_delete_blob_finish(seq, blob, 0);
8492 : 2890 : free(ctx);
8493 : : }
8494 : : }
8495 : :
8496 : : void
8497 : 3130 : spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
8498 : : spdk_blob_op_complete cb_fn, void *cb_arg)
8499 : : {
8500 : 3130 : struct spdk_bs_cpl cpl;
8501 : : spdk_bs_sequence_t *seq;
8502 : :
8503 [ - + ]: 3130 : SPDK_DEBUGLOG(blob, "Deleting blob 0x%" PRIx64 "\n", blobid);
8504 : :
8505 [ - + ]: 3130 : assert(spdk_get_thread() == bs->md_thread);
8506 : :
8507 : 3130 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
8508 : 3130 : cpl.u.blob_basic.cb_fn = cb_fn;
8509 : 3130 : cpl.u.blob_basic.cb_arg = cb_arg;
8510 : :
8511 : 3130 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
8512 [ - + ]: 3130 : if (!seq) {
8513 : 0 : cb_fn(cb_arg, -ENOMEM);
8514 : 0 : return;
8515 : : }
8516 : :
8517 : 3130 : spdk_bs_open_blob(bs, blobid, bs_delete_open_cpl, seq);
8518 : : }
8519 : :
8520 : : /* END spdk_bs_delete_blob */
8521 : :
8522 : : /* START spdk_bs_open_blob */
8523 : :
8524 : : static void
8525 : 7028 : bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8526 : : {
8527 : 7028 : struct spdk_blob *blob = cb_arg;
8528 : : struct spdk_blob *existing;
8529 : :
8530 [ + + ]: 7028 : if (bserrno != 0) {
8531 : 128 : blob_free(blob);
8532 : 128 : seq->cpl.u.blob_handle.blob = NULL;
8533 : 128 : bs_sequence_finish(seq, bserrno);
8534 : 128 : return;
8535 : : }
8536 : :
8537 : 6900 : existing = blob_lookup(blob->bs, blob->id);
8538 [ + + ]: 6900 : if (existing) {
8539 : 12 : blob_free(blob);
8540 : 12 : existing->open_ref++;
8541 : 12 : seq->cpl.u.blob_handle.blob = existing;
8542 : 12 : bs_sequence_finish(seq, 0);
8543 : 12 : return;
8544 : : }
8545 : :
8546 : 6888 : blob->open_ref++;
8547 : :
8548 : 6888 : spdk_bit_array_set(blob->bs->open_blobids, blob->id);
8549 : 6888 : RB_INSERT(spdk_blob_tree, &blob->bs->open_blobs, blob);
8550 : :
8551 : 6888 : bs_sequence_finish(seq, bserrno);
8552 : : }
8553 : :
8554 : : static inline void
8555 : 31 : blob_open_opts_copy(const struct spdk_blob_open_opts *src, struct spdk_blob_open_opts *dst)
8556 : : {
8557 : : #define FIELD_OK(field) \
8558 : : offsetof(struct spdk_blob_open_opts, field) + sizeof(src->field) <= src->opts_size
8559 : :
8560 : : #define SET_FIELD(field) \
8561 : : if (FIELD_OK(field)) { \
8562 : : dst->field = src->field; \
8563 : : } \
8564 : :
8565 [ + - ]: 31 : SET_FIELD(clear_method);
8566 [ + - ]: 31 : SET_FIELD(esnap_ctx);
8567 : :
8568 : 31 : dst->opts_size = src->opts_size;
8569 : :
8570 : : /* You should not remove this statement, but need to update the assert statement
8571 : : * if you add a new field, and also add a corresponding SET_FIELD statement */
8572 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_open_opts) == 24, "Incorrect size");
8573 : :
8574 : : #undef FIELD_OK
8575 : : #undef SET_FIELD
8576 : 31 : }
8577 : :
8578 : : static void
8579 : 8636 : bs_open_blob(struct spdk_blob_store *bs,
8580 : : spdk_blob_id blobid,
8581 : : struct spdk_blob_open_opts *opts,
8582 : : spdk_blob_op_with_handle_complete cb_fn,
8583 : : void *cb_arg)
8584 : : {
8585 : : struct spdk_blob *blob;
8586 : 8628 : struct spdk_bs_cpl cpl;
8587 : 8628 : struct spdk_blob_open_opts opts_local;
8588 : : spdk_bs_sequence_t *seq;
8589 : : uint32_t page_num;
8590 : :
8591 [ - + ]: 8636 : SPDK_DEBUGLOG(blob, "Opening blob 0x%" PRIx64 "\n", blobid);
8592 [ - + ]: 8636 : assert(spdk_get_thread() == bs->md_thread);
8593 : :
8594 : 8636 : page_num = bs_blobid_to_page(blobid);
8595 [ + + ]: 8636 : if (spdk_bit_array_get(bs->used_blobids, page_num) == false) {
8596 : : /* Invalid blobid */
8597 : 96 : cb_fn(cb_arg, NULL, -ENOENT);
8598 : 96 : return;
8599 : : }
8600 : :
8601 : 8540 : blob = blob_lookup(bs, blobid);
8602 [ + + ]: 8540 : if (blob) {
8603 : 1512 : blob->open_ref++;
8604 : 1512 : cb_fn(cb_arg, blob, 0);
8605 : 1512 : return;
8606 : : }
8607 : :
8608 : 7028 : blob = blob_alloc(bs, blobid);
8609 [ - + ]: 7028 : if (!blob) {
8610 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
8611 : 0 : return;
8612 : : }
8613 : :
8614 : 7028 : spdk_blob_open_opts_init(&opts_local, sizeof(opts_local));
8615 [ + + ]: 7028 : if (opts) {
8616 : 31 : blob_open_opts_copy(opts, &opts_local);
8617 : : }
8618 : :
8619 : 7028 : blob->clear_method = opts_local.clear_method;
8620 : :
8621 : 7028 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
8622 : 7028 : cpl.u.blob_handle.cb_fn = cb_fn;
8623 : 7028 : cpl.u.blob_handle.cb_arg = cb_arg;
8624 : 7028 : cpl.u.blob_handle.blob = blob;
8625 : 7028 : cpl.u.blob_handle.esnap_ctx = opts_local.esnap_ctx;
8626 : :
8627 : 7028 : seq = bs_sequence_start_bs(bs->md_channel, &cpl);
8628 [ - + ]: 7028 : if (!seq) {
8629 : 0 : blob_free(blob);
8630 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
8631 : 0 : return;
8632 : : }
8633 : :
8634 : 7028 : blob_load(seq, blob, bs_open_blob_cpl, blob);
8635 : : }
8636 : :
8637 : : void
8638 : 8603 : spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
8639 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
8640 : : {
8641 : 8603 : bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg);
8642 : 8603 : }
8643 : :
8644 : : void
8645 : 33 : spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid,
8646 : : struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
8647 : : {
8648 : 33 : bs_open_blob(bs, blobid, opts, cb_fn, cb_arg);
8649 : 33 : }
8650 : :
8651 : : /* END spdk_bs_open_blob */
8652 : :
8653 : : /* START spdk_blob_set_read_only */
8654 : : int
8655 : 466 : spdk_blob_set_read_only(struct spdk_blob *blob)
8656 : : {
8657 : 466 : blob_verify_md_op(blob);
8658 : :
8659 : 466 : blob->data_ro_flags |= SPDK_BLOB_READ_ONLY;
8660 : :
8661 : 466 : blob->state = SPDK_BLOB_STATE_DIRTY;
8662 : 466 : return 0;
8663 : : }
8664 : : /* END spdk_blob_set_read_only */
8665 : :
8666 : : /* START spdk_blob_sync_md */
8667 : :
8668 : : static void
8669 : 3199 : blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8670 : : {
8671 : 3199 : struct spdk_blob *blob = cb_arg;
8672 : :
8673 [ + - + + ]: 3199 : if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
8674 : 802 : blob->data_ro = true;
8675 : 802 : blob->md_ro = true;
8676 : : }
8677 : :
8678 : 3199 : bs_sequence_finish(seq, bserrno);
8679 : 3199 : }
8680 : :
8681 : : static void
8682 : 3199 : blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
8683 : : {
8684 : 3195 : struct spdk_bs_cpl cpl;
8685 : : spdk_bs_sequence_t *seq;
8686 : :
8687 : 3199 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
8688 : 3199 : cpl.u.blob_basic.cb_fn = cb_fn;
8689 : 3199 : cpl.u.blob_basic.cb_arg = cb_arg;
8690 : :
8691 : 3199 : seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
8692 [ - + ]: 3199 : if (!seq) {
8693 : 0 : cb_fn(cb_arg, -ENOMEM);
8694 : 0 : return;
8695 : : }
8696 : :
8697 : 3199 : blob_persist(seq, blob, blob_sync_md_cpl, blob);
8698 : : }
8699 : :
8700 : : void
8701 : 2177 : spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
8702 : : {
8703 : 2177 : blob_verify_md_op(blob);
8704 : :
8705 [ - + ]: 2177 : SPDK_DEBUGLOG(blob, "Syncing blob 0x%" PRIx64 "\n", blob->id);
8706 : :
8707 [ + + ]: 2177 : if (blob->md_ro) {
8708 [ - + ]: 8 : assert(blob->state == SPDK_BLOB_STATE_CLEAN);
8709 : 8 : cb_fn(cb_arg, 0);
8710 : 8 : return;
8711 : : }
8712 : :
8713 : 2169 : blob_sync_md(blob, cb_fn, cb_arg);
8714 : : }
8715 : :
8716 : : /* END spdk_blob_sync_md */
8717 : :
8718 : : struct spdk_blob_cluster_op_ctx {
8719 : : struct spdk_thread *thread;
8720 : : struct spdk_blob *blob;
8721 : : uint32_t cluster_num; /* cluster index in blob */
8722 : : uint32_t cluster; /* cluster on disk */
8723 : : uint32_t extent_page; /* extent page on disk */
8724 : : struct spdk_blob_md_page *page; /* preallocated extent page */
8725 : : int rc;
8726 : : spdk_blob_op_complete cb_fn;
8727 : : void *cb_arg;
8728 : : };
8729 : :
8730 : : static void
8731 : 1758 : blob_op_cluster_msg_cpl(void *arg)
8732 : : {
8733 : 1758 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8734 : :
8735 : 1758 : ctx->cb_fn(ctx->cb_arg, ctx->rc);
8736 : 1758 : free(ctx);
8737 : 1758 : }
8738 : :
8739 : : static void
8740 : 1698 : blob_op_cluster_msg_cb(void *arg, int bserrno)
8741 : : {
8742 : 1698 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8743 : :
8744 : 1698 : ctx->rc = bserrno;
8745 : 1698 : spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
8746 : 1698 : }
8747 : :
8748 : : static void
8749 : 166 : blob_insert_new_ep_cb(void *arg, int bserrno)
8750 : : {
8751 : 166 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8752 : : uint32_t *extent_page;
8753 : :
8754 : 166 : extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
8755 : 166 : *extent_page = ctx->extent_page;
8756 : 166 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8757 : 166 : blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
8758 : 166 : }
8759 : :
8760 : : struct spdk_blob_write_extent_page_ctx {
8761 : : struct spdk_blob_store *bs;
8762 : :
8763 : : uint32_t extent;
8764 : : struct spdk_blob_md_page *page;
8765 : : };
8766 : :
8767 : : static void
8768 : 52 : blob_free_cluster_msg_cb(void *arg, int bserrno)
8769 : : {
8770 : 52 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8771 : :
8772 : 52 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8773 : 52 : bs_release_cluster(ctx->blob->bs, ctx->cluster);
8774 : 52 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8775 : :
8776 : 52 : ctx->rc = bserrno;
8777 : 52 : spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
8778 : 52 : }
8779 : :
8780 : : static void
8781 : 52 : blob_free_cluster_update_ep_cb(void *arg, int bserrno)
8782 : : {
8783 : 52 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8784 : :
8785 [ + - + - ]: 52 : if (bserrno != 0 || ctx->blob->bs->clean == 0) {
8786 : 52 : blob_free_cluster_msg_cb(ctx, bserrno);
8787 : 52 : return;
8788 : : }
8789 : :
8790 : 0 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8791 : 0 : blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
8792 : : }
8793 : :
8794 : : static void
8795 : 0 : blob_free_cluster_free_ep_cb(void *arg, int bserrno)
8796 : : {
8797 : 0 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8798 : :
8799 : 0 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8800 [ # # ]: 0 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8801 : 0 : bs_release_md_page(ctx->blob->bs, ctx->extent_page);
8802 : 0 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8803 : 0 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8804 : 0 : blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
8805 : 0 : }
8806 : :
8807 : : static void
8808 : 874 : blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8809 : : {
8810 : 874 : struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
8811 : :
8812 : 874 : free(ctx);
8813 : 874 : bs_sequence_finish(seq, bserrno);
8814 : 874 : }
8815 : :
8816 : : static void
8817 : 874 : blob_write_extent_page_ready(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
8818 : : {
8819 : 874 : struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
8820 : :
8821 [ - + ]: 874 : if (bserrno != 0) {
8822 : 0 : blob_persist_extent_page_cpl(seq, ctx, bserrno);
8823 : 0 : return;
8824 : : }
8825 : 874 : bs_sequence_write_dev(seq, ctx->page, bs_md_page_to_lba(ctx->bs, ctx->extent),
8826 : 874 : bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
8827 : : blob_persist_extent_page_cpl, ctx);
8828 : : }
8829 : :
8830 : : static void
8831 : 874 : blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
8832 : : struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
8833 : : {
8834 : : struct spdk_blob_write_extent_page_ctx *ctx;
8835 : : spdk_bs_sequence_t *seq;
8836 : 874 : struct spdk_bs_cpl cpl;
8837 : :
8838 : 874 : ctx = calloc(1, sizeof(*ctx));
8839 [ - + ]: 874 : if (!ctx) {
8840 : 0 : cb_fn(cb_arg, -ENOMEM);
8841 : 0 : return;
8842 : : }
8843 : 874 : ctx->bs = blob->bs;
8844 : 874 : ctx->extent = extent;
8845 : 874 : ctx->page = page;
8846 : :
8847 : 874 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
8848 : 874 : cpl.u.blob_basic.cb_fn = cb_fn;
8849 : 874 : cpl.u.blob_basic.cb_arg = cb_arg;
8850 : :
8851 : 874 : seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
8852 [ - + ]: 874 : if (!seq) {
8853 : 0 : free(ctx);
8854 : 0 : cb_fn(cb_arg, -ENOMEM);
8855 : 0 : return;
8856 : : }
8857 : :
8858 [ - + ]: 874 : assert(page);
8859 : 874 : page->next = SPDK_INVALID_MD_PAGE;
8860 : 874 : page->id = blob->id;
8861 : 874 : page->sequence_num = 0;
8862 : :
8863 : 874 : blob_serialize_extent_page(blob, cluster_num, page);
8864 : :
8865 : 874 : page->crc = blob_md_page_calc_crc(page);
8866 : :
8867 [ - + ]: 874 : assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true);
8868 : :
8869 : 874 : bs_mark_dirty(seq, blob->bs, blob_write_extent_page_ready, ctx);
8870 : : }
8871 : :
8872 : : static void
8873 : 1638 : blob_insert_cluster_msg(void *arg)
8874 : : {
8875 : 1638 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8876 : : uint32_t *extent_page;
8877 : :
8878 : 1638 : ctx->rc = blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster);
8879 [ + + ]: 1638 : if (ctx->rc != 0) {
8880 : 8 : spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
8881 : 8 : return;
8882 : : }
8883 : :
8884 [ + + ]: 1630 : if (ctx->blob->use_extent_table == false) {
8885 : : /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
8886 : 812 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8887 : 812 : blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
8888 : 812 : return;
8889 : : }
8890 : :
8891 : 818 : extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
8892 [ + + ]: 818 : if (*extent_page == 0) {
8893 : : /* Extent page requires allocation.
8894 : : * It was already claimed in the used_md_pages map and placed in ctx. */
8895 [ - + ]: 166 : assert(ctx->extent_page != 0);
8896 [ - + ]: 166 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8897 : 166 : blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
8898 : : blob_insert_new_ep_cb, ctx);
8899 : : } else {
8900 : : /* It is possible for original thread to allocate extent page for
8901 : : * different cluster in the same extent page. In such case proceed with
8902 : : * updating the existing extent page, but release the additional one. */
8903 [ - + ]: 652 : if (ctx->extent_page != 0) {
8904 : 0 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8905 [ # # ]: 0 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8906 : 0 : bs_release_md_page(ctx->blob->bs, ctx->extent_page);
8907 : 0 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8908 : 0 : ctx->extent_page = 0;
8909 : : }
8910 : : /* Extent page already allocated.
8911 : : * Every cluster allocation, requires just an update of single extent page. */
8912 : 652 : blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
8913 : : blob_op_cluster_msg_cb, ctx);
8914 : : }
8915 : : }
8916 : :
8917 : : static void
8918 : 1638 : blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
8919 : : uint64_t cluster, uint32_t extent_page, struct spdk_blob_md_page *page,
8920 : : spdk_blob_op_complete cb_fn, void *cb_arg)
8921 : : {
8922 : : struct spdk_blob_cluster_op_ctx *ctx;
8923 : :
8924 : 1638 : ctx = calloc(1, sizeof(*ctx));
8925 [ - + ]: 1638 : if (ctx == NULL) {
8926 : 0 : cb_fn(cb_arg, -ENOMEM);
8927 : 0 : return;
8928 : : }
8929 : :
8930 : 1638 : ctx->thread = spdk_get_thread();
8931 : 1638 : ctx->blob = blob;
8932 : 1638 : ctx->cluster_num = cluster_num;
8933 : 1638 : ctx->cluster = cluster;
8934 : 1638 : ctx->extent_page = extent_page;
8935 : 1638 : ctx->page = page;
8936 : 1638 : ctx->cb_fn = cb_fn;
8937 : 1638 : ctx->cb_arg = cb_arg;
8938 : :
8939 : 1638 : spdk_thread_send_msg(blob->bs->md_thread, blob_insert_cluster_msg, ctx);
8940 : : }
8941 : :
8942 : : static void
8943 : 120 : blob_free_cluster_msg(void *arg)
8944 : : {
8945 : 120 : struct spdk_blob_cluster_op_ctx *ctx = arg;
8946 : : uint32_t *extent_page;
8947 : : uint32_t start_cluster_idx;
8948 : 120 : bool free_extent_page = true;
8949 : : size_t i;
8950 : :
8951 : 120 : ctx->cluster = bs_lba_to_cluster(ctx->blob->bs, ctx->blob->active.clusters[ctx->cluster_num]);
8952 : :
8953 : : /* There were concurrent unmaps to the same cluster, only release the cluster on the first one */
8954 [ + + ]: 120 : if (ctx->cluster == 0) {
8955 : 16 : blob_op_cluster_msg_cb(ctx, 0);
8956 : 16 : return;
8957 : : }
8958 : :
8959 : 104 : ctx->blob->active.clusters[ctx->cluster_num] = 0;
8960 [ + - ]: 104 : if (ctx->cluster != 0) {
8961 : 104 : ctx->blob->active.num_allocated_clusters--;
8962 : : }
8963 : :
8964 [ + + ]: 104 : if (ctx->blob->use_extent_table == false) {
8965 : : /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
8966 : 52 : spdk_spin_lock(&ctx->blob->bs->used_lock);
8967 : 52 : bs_release_cluster(ctx->blob->bs, ctx->cluster);
8968 : 52 : spdk_spin_unlock(&ctx->blob->bs->used_lock);
8969 : 52 : ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
8970 : 52 : blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
8971 : 52 : return;
8972 : : }
8973 : :
8974 : 52 : extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
8975 : :
8976 : : /* There shouldn't be parallel release operations on same cluster */
8977 [ - + ]: 52 : assert(*extent_page == ctx->extent_page);
8978 : :
8979 : 52 : start_cluster_idx = (ctx->cluster_num / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
8980 [ + - ]: 96 : for (i = 0; i < SPDK_EXTENTS_PER_EP; ++i) {
8981 [ + + ]: 96 : if (ctx->blob->active.clusters[start_cluster_idx + i] != 0) {
8982 : 52 : free_extent_page = false;
8983 : 52 : break;
8984 : : }
8985 : : }
8986 : :
8987 [ - + ]: 52 : if (free_extent_page) {
8988 [ # # ]: 0 : assert(ctx->extent_page != 0);
8989 [ # # ]: 0 : assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
8990 : 0 : ctx->blob->active.extent_pages[bs_cluster_to_extent_table_id(ctx->cluster_num)] = 0;
8991 : 0 : blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
8992 : : blob_free_cluster_free_ep_cb, ctx);
8993 : : } else {
8994 : 52 : blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
8995 : : blob_free_cluster_update_ep_cb, ctx);
8996 : : }
8997 : : }
8998 : :
8999 : :
9000 : : static void
9001 : 120 : blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, uint32_t extent_page,
9002 : : struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
9003 : : {
9004 : : struct spdk_blob_cluster_op_ctx *ctx;
9005 : :
9006 : 120 : ctx = calloc(1, sizeof(*ctx));
9007 [ - + ]: 120 : if (ctx == NULL) {
9008 : 0 : cb_fn(cb_arg, -ENOMEM);
9009 : 0 : return;
9010 : : }
9011 : :
9012 : 120 : ctx->thread = spdk_get_thread();
9013 : 120 : ctx->blob = blob;
9014 : 120 : ctx->cluster_num = cluster_num;
9015 : 120 : ctx->extent_page = extent_page;
9016 : 120 : ctx->page = page;
9017 : 120 : ctx->cb_fn = cb_fn;
9018 : 120 : ctx->cb_arg = cb_arg;
9019 : :
9020 : 120 : spdk_thread_send_msg(blob->bs->md_thread, blob_free_cluster_msg, ctx);
9021 : : }
9022 : :
9023 : : /* START spdk_blob_close */
9024 : :
9025 : : static void
9026 : 8412 : blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9027 : : {
9028 : 8412 : struct spdk_blob *blob = cb_arg;
9029 : :
9030 [ + - ]: 8412 : if (bserrno == 0) {
9031 : 8412 : blob->open_ref--;
9032 [ + + ]: 8412 : if (blob->open_ref == 0) {
9033 : : /*
9034 : : * Blobs with active.num_pages == 0 are deleted blobs.
9035 : : * these blobs are removed from the blob_store list
9036 : : * when the deletion process starts - so don't try to
9037 : : * remove them again.
9038 : : */
9039 [ + + ]: 6888 : if (blob->active.num_pages > 0) {
9040 : 3902 : spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
9041 : 3902 : RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
9042 : : }
9043 : 6888 : blob_free(blob);
9044 : : }
9045 : : }
9046 : :
9047 : 8412 : bs_sequence_finish(seq, bserrno);
9048 : 8412 : }
9049 : :
9050 : : static void
9051 : 224 : blob_close_esnap_done(void *cb_arg, struct spdk_blob *blob, int bserrno)
9052 : : {
9053 : 224 : spdk_bs_sequence_t *seq = cb_arg;
9054 : :
9055 [ - + ]: 224 : if (bserrno != 0) {
9056 [ # # ]: 0 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": close failed with error %d\n",
9057 : : blob->id, bserrno);
9058 : 0 : bs_sequence_finish(seq, bserrno);
9059 : 0 : return;
9060 : : }
9061 : :
9062 [ - + ]: 224 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": closed, syncing metadata on thread %s\n",
9063 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
9064 : :
9065 : : /* Sync metadata */
9066 : 224 : blob_persist(seq, blob, blob_close_cpl, blob);
9067 : : }
9068 : :
9069 : : void
9070 : 8412 : spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
9071 : : {
9072 : 8404 : struct spdk_bs_cpl cpl;
9073 : : spdk_bs_sequence_t *seq;
9074 : :
9075 : 8412 : blob_verify_md_op(blob);
9076 : :
9077 [ - + ]: 8412 : SPDK_DEBUGLOG(blob, "Closing blob 0x%" PRIx64 "\n", blob->id);
9078 : :
9079 [ - + ]: 8412 : if (blob->open_ref == 0) {
9080 : 0 : cb_fn(cb_arg, -EBADF);
9081 : 0 : return;
9082 : : }
9083 : :
9084 : 8412 : cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
9085 : 8412 : cpl.u.blob_basic.cb_fn = cb_fn;
9086 : 8412 : cpl.u.blob_basic.cb_arg = cb_arg;
9087 : :
9088 : 8412 : seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
9089 [ - + ]: 8412 : if (!seq) {
9090 : 0 : cb_fn(cb_arg, -ENOMEM);
9091 : 0 : return;
9092 : : }
9093 : :
9094 [ + + + + ]: 8412 : if (blob->open_ref == 1 && blob_is_esnap_clone(blob)) {
9095 : 224 : blob_esnap_destroy_bs_dev_channels(blob, false, blob_close_esnap_done, seq);
9096 : 224 : return;
9097 : : }
9098 : :
9099 : : /* Sync metadata */
9100 : 8188 : blob_persist(seq, blob, blob_close_cpl, blob);
9101 : : }
9102 : :
9103 : : /* END spdk_blob_close */
9104 : :
9105 : 492 : struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
9106 : : {
9107 : 492 : return spdk_get_io_channel(bs);
9108 : : }
9109 : :
9110 : : void
9111 : 458 : spdk_bs_free_io_channel(struct spdk_io_channel *channel)
9112 : : {
9113 : 458 : blob_esnap_destroy_bs_channel(spdk_io_channel_get_ctx(channel));
9114 : 458 : spdk_put_io_channel(channel);
9115 : 458 : }
9116 : :
9117 : : void
9118 : 226 : spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
9119 : : uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
9120 : : {
9121 : 226 : blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
9122 : : SPDK_BLOB_UNMAP);
9123 : 226 : }
9124 : :
9125 : : void
9126 : 96 : spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
9127 : : uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
9128 : : {
9129 : 96 : blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
9130 : : SPDK_BLOB_WRITE_ZEROES);
9131 : 96 : }
9132 : :
9133 : : void
9134 : 41736 : spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
9135 : : void *payload, uint64_t offset, uint64_t length,
9136 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9137 : : {
9138 : 41736 : blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
9139 : : SPDK_BLOB_WRITE);
9140 : 41736 : }
9141 : :
9142 : : void
9143 : 35000 : spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
9144 : : void *payload, uint64_t offset, uint64_t length,
9145 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9146 : : {
9147 : 35000 : blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
9148 : : SPDK_BLOB_READ);
9149 : 35000 : }
9150 : :
9151 : : void
9152 : 280 : spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
9153 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9154 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9155 : : {
9156 : 280 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false, NULL);
9157 : 280 : }
9158 : :
9159 : : void
9160 : 1880 : spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
9161 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9162 : : spdk_blob_op_complete cb_fn, void *cb_arg)
9163 : : {
9164 : 1880 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true, NULL);
9165 : 1880 : }
9166 : :
9167 : : void
9168 : 949 : spdk_blob_io_writev_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
9169 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9170 : : spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
9171 : : {
9172 : 949 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false,
9173 : : io_opts);
9174 : 949 : }
9175 : :
9176 : : void
9177 : 3255 : spdk_blob_io_readv_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
9178 : : struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
9179 : : spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
9180 : : {
9181 : 3255 : blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true,
9182 : : io_opts);
9183 : 3255 : }
9184 : :
9185 : : struct spdk_bs_iter_ctx {
9186 : : int64_t page_num;
9187 : : struct spdk_blob_store *bs;
9188 : :
9189 : : spdk_blob_op_with_handle_complete cb_fn;
9190 : : void *cb_arg;
9191 : : };
9192 : :
9193 : : static void
9194 : 2404 : bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
9195 : : {
9196 : 2404 : struct spdk_bs_iter_ctx *ctx = cb_arg;
9197 : 2404 : struct spdk_blob_store *bs = ctx->bs;
9198 : : spdk_blob_id id;
9199 : :
9200 [ + + ]: 2404 : if (bserrno == 0) {
9201 : 920 : ctx->cb_fn(ctx->cb_arg, _blob, bserrno);
9202 : 920 : free(ctx);
9203 : 920 : return;
9204 : : }
9205 : :
9206 : 1484 : ctx->page_num++;
9207 : 1484 : ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num);
9208 [ + + ]: 1484 : if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) {
9209 : 548 : ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
9210 : 548 : free(ctx);
9211 : 548 : return;
9212 : : }
9213 : :
9214 : 936 : id = bs_page_to_blobid(ctx->page_num);
9215 : :
9216 : 936 : spdk_bs_open_blob(bs, id, bs_iter_cpl, ctx);
9217 : : }
9218 : :
9219 : : void
9220 : 596 : spdk_bs_iter_first(struct spdk_blob_store *bs,
9221 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
9222 : : {
9223 : : struct spdk_bs_iter_ctx *ctx;
9224 : :
9225 : 596 : ctx = calloc(1, sizeof(*ctx));
9226 [ - + ]: 596 : if (!ctx) {
9227 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
9228 : 0 : return;
9229 : : }
9230 : :
9231 : 596 : ctx->page_num = -1;
9232 : 596 : ctx->bs = bs;
9233 : 596 : ctx->cb_fn = cb_fn;
9234 : 596 : ctx->cb_arg = cb_arg;
9235 : :
9236 : 596 : bs_iter_cpl(ctx, NULL, -1);
9237 : : }
9238 : :
9239 : : static void
9240 : 872 : bs_iter_close_cpl(void *cb_arg, int bserrno)
9241 : : {
9242 : 872 : struct spdk_bs_iter_ctx *ctx = cb_arg;
9243 : :
9244 : 872 : bs_iter_cpl(ctx, NULL, -1);
9245 : 872 : }
9246 : :
9247 : : void
9248 : 872 : spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob,
9249 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
9250 : : {
9251 : : struct spdk_bs_iter_ctx *ctx;
9252 : :
9253 [ - + ]: 872 : assert(blob != NULL);
9254 : :
9255 : 872 : ctx = calloc(1, sizeof(*ctx));
9256 [ - + ]: 872 : if (!ctx) {
9257 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
9258 : 0 : return;
9259 : : }
9260 : :
9261 : 872 : ctx->page_num = bs_blobid_to_page(blob->id);
9262 : 872 : ctx->bs = bs;
9263 : 872 : ctx->cb_fn = cb_fn;
9264 : 872 : ctx->cb_arg = cb_arg;
9265 : :
9266 : : /* Close the existing blob */
9267 : 872 : spdk_blob_close(blob, bs_iter_close_cpl, ctx);
9268 : : }
9269 : :
9270 : : static int
9271 : 1940 : blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
9272 : : uint16_t value_len, bool internal)
9273 : : {
9274 : : struct spdk_xattr_tailq *xattrs;
9275 : : struct spdk_xattr *xattr;
9276 : : size_t desc_size;
9277 : : void *tmp;
9278 : :
9279 : 1940 : blob_verify_md_op(blob);
9280 : :
9281 [ + + ]: 1940 : if (blob->md_ro) {
9282 : 8 : return -EPERM;
9283 : : }
9284 : :
9285 : 1932 : desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len;
9286 [ + + ]: 1932 : if (desc_size > SPDK_BS_MAX_DESC_SIZE) {
9287 [ - + ]: 8 : SPDK_DEBUGLOG(blob, "Xattr '%s' of size %zu does not fix into single page %zu\n", name,
9288 : : desc_size, SPDK_BS_MAX_DESC_SIZE);
9289 : 8 : return -ENOMEM;
9290 : : }
9291 : :
9292 [ + + ]: 1924 : if (internal) {
9293 : 1454 : xattrs = &blob->xattrs_internal;
9294 : 1454 : blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR;
9295 : : } else {
9296 : 470 : xattrs = &blob->xattrs;
9297 : : }
9298 : :
9299 [ + + ]: 2394 : TAILQ_FOREACH(xattr, xattrs, link) {
9300 [ + + ]: 688 : if (!strcmp(name, xattr->name)) {
9301 : 218 : tmp = malloc(value_len);
9302 [ - + ]: 218 : if (!tmp) {
9303 : 0 : return -ENOMEM;
9304 : : }
9305 : :
9306 : 218 : free(xattr->value);
9307 : 218 : xattr->value_len = value_len;
9308 : 218 : xattr->value = tmp;
9309 : 218 : memcpy(xattr->value, value, value_len);
9310 : :
9311 : 218 : blob->state = SPDK_BLOB_STATE_DIRTY;
9312 : :
9313 : 218 : return 0;
9314 : : }
9315 : : }
9316 : :
9317 : 1706 : xattr = calloc(1, sizeof(*xattr));
9318 [ - + ]: 1706 : if (!xattr) {
9319 : 0 : return -ENOMEM;
9320 : : }
9321 : :
9322 : 1706 : xattr->name = strdup(name);
9323 [ - + ]: 1706 : if (!xattr->name) {
9324 : 0 : free(xattr);
9325 : 0 : return -ENOMEM;
9326 : : }
9327 : :
9328 : 1706 : xattr->value_len = value_len;
9329 : 1706 : xattr->value = malloc(value_len);
9330 [ - + ]: 1706 : if (!xattr->value) {
9331 : 0 : free(xattr->name);
9332 : 0 : free(xattr);
9333 : 0 : return -ENOMEM;
9334 : : }
9335 : 1706 : memcpy(xattr->value, value, value_len);
9336 : 1706 : TAILQ_INSERT_TAIL(xattrs, xattr, link);
9337 : :
9338 : 1706 : blob->state = SPDK_BLOB_STATE_DIRTY;
9339 : :
9340 : 1706 : return 0;
9341 : : }
9342 : :
9343 : : int
9344 : 384 : spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
9345 : : uint16_t value_len)
9346 : : {
9347 : 384 : return blob_set_xattr(blob, name, value, value_len, false);
9348 : : }
9349 : :
9350 : : static int
9351 : 810 : blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal)
9352 : : {
9353 : : struct spdk_xattr_tailq *xattrs;
9354 : : struct spdk_xattr *xattr;
9355 : :
9356 : 810 : blob_verify_md_op(blob);
9357 : :
9358 [ + + ]: 810 : if (blob->md_ro) {
9359 : 8 : return -EPERM;
9360 : : }
9361 [ + + ]: 802 : xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
9362 : :
9363 [ + + ]: 826 : TAILQ_FOREACH(xattr, xattrs, link) {
9364 [ + + ]: 722 : if (!strcmp(name, xattr->name)) {
9365 [ + + ]: 698 : TAILQ_REMOVE(xattrs, xattr, link);
9366 : 698 : free(xattr->value);
9367 : 698 : free(xattr->name);
9368 : 698 : free(xattr);
9369 : :
9370 [ + + + + ]: 698 : if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) {
9371 : 482 : blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR;
9372 : : }
9373 : 698 : blob->state = SPDK_BLOB_STATE_DIRTY;
9374 : :
9375 : 698 : return 0;
9376 : : }
9377 : : }
9378 : :
9379 : 104 : return -ENOENT;
9380 : : }
9381 : :
9382 : : int
9383 : 72 : spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name)
9384 : : {
9385 : 72 : return blob_remove_xattr(blob, name, false);
9386 : : }
9387 : :
9388 : : static int
9389 : 4640 : blob_get_xattr_value(struct spdk_blob *blob, const char *name,
9390 : : const void **value, size_t *value_len, bool internal)
9391 : : {
9392 : : struct spdk_xattr *xattr;
9393 : : struct spdk_xattr_tailq *xattrs;
9394 : :
9395 [ + + ]: 4640 : xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
9396 : :
9397 [ + + ]: 5910 : TAILQ_FOREACH(xattr, xattrs, link) {
9398 [ + + ]: 2822 : if (!strcmp(name, xattr->name)) {
9399 : 1552 : *value = xattr->value;
9400 : 1552 : *value_len = xattr->value_len;
9401 : 1552 : return 0;
9402 : : }
9403 : : }
9404 : 3088 : return -ENOENT;
9405 : : }
9406 : :
9407 : : int
9408 : 340 : spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
9409 : : const void **value, size_t *value_len)
9410 : : {
9411 : 340 : blob_verify_md_op(blob);
9412 : :
9413 : 340 : return blob_get_xattr_value(blob, name, value, value_len, false);
9414 : : }
9415 : :
9416 : : struct spdk_xattr_names {
9417 : : uint32_t count;
9418 : : const char *names[0];
9419 : : };
9420 : :
9421 : : static int
9422 : 8 : blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names)
9423 : : {
9424 : : struct spdk_xattr *xattr;
9425 : 8 : int count = 0;
9426 : :
9427 [ + + ]: 24 : TAILQ_FOREACH(xattr, xattrs, link) {
9428 : 16 : count++;
9429 : : }
9430 : :
9431 : 8 : *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
9432 [ - + ]: 8 : if (*names == NULL) {
9433 : 0 : return -ENOMEM;
9434 : : }
9435 : :
9436 [ + + ]: 24 : TAILQ_FOREACH(xattr, xattrs, link) {
9437 : 16 : (*names)->names[(*names)->count++] = xattr->name;
9438 : : }
9439 : :
9440 : 8 : return 0;
9441 : : }
9442 : :
9443 : : int
9444 : 8 : spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names)
9445 : : {
9446 : 8 : blob_verify_md_op(blob);
9447 : :
9448 : 8 : return blob_get_xattr_names(&blob->xattrs, names);
9449 : : }
9450 : :
9451 : : uint32_t
9452 : 8 : spdk_xattr_names_get_count(struct spdk_xattr_names *names)
9453 : : {
9454 [ - + ]: 8 : assert(names != NULL);
9455 : :
9456 : 8 : return names->count;
9457 : : }
9458 : :
9459 : : const char *
9460 : 16 : spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
9461 : : {
9462 [ - + ]: 16 : if (index >= names->count) {
9463 : 0 : return NULL;
9464 : : }
9465 : :
9466 : 16 : return names->names[index];
9467 : : }
9468 : :
9469 : : void
9470 : 8 : spdk_xattr_names_free(struct spdk_xattr_names *names)
9471 : : {
9472 : 8 : free(names);
9473 : 8 : }
9474 : :
9475 : : struct spdk_bs_type
9476 : 4 : spdk_bs_get_bstype(struct spdk_blob_store *bs)
9477 : : {
9478 : 4 : return bs->bstype;
9479 : : }
9480 : :
9481 : : void
9482 : 0 : spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
9483 : : {
9484 : 0 : memcpy(&bs->bstype, &bstype, sizeof(bstype));
9485 : 0 : }
9486 : :
9487 : : bool
9488 : 152 : spdk_blob_is_read_only(struct spdk_blob *blob)
9489 : : {
9490 [ - + ]: 152 : assert(blob != NULL);
9491 [ + + - + ]: 152 : return (blob->data_ro || blob->md_ro);
9492 : : }
9493 : :
9494 : : bool
9495 : 120 : spdk_blob_is_snapshot(struct spdk_blob *blob)
9496 : : {
9497 : : struct spdk_blob_list *snapshot_entry;
9498 : :
9499 [ - + ]: 120 : assert(blob != NULL);
9500 : :
9501 : 120 : snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
9502 [ + + ]: 120 : if (snapshot_entry == NULL) {
9503 : 68 : return false;
9504 : : }
9505 : :
9506 : 52 : return true;
9507 : : }
9508 : :
9509 : : bool
9510 : 152 : spdk_blob_is_clone(struct spdk_blob *blob)
9511 : : {
9512 [ - + ]: 152 : assert(blob != NULL);
9513 : :
9514 [ + + ]: 152 : if (blob->parent_id != SPDK_BLOBID_INVALID &&
9515 [ + + ]: 96 : blob->parent_id != SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
9516 [ - + ]: 88 : assert(spdk_blob_is_thin_provisioned(blob));
9517 : 88 : return true;
9518 : : }
9519 : :
9520 : 64 : return false;
9521 : : }
9522 : :
9523 : : bool
9524 : 73506 : spdk_blob_is_thin_provisioned(struct spdk_blob *blob)
9525 : : {
9526 [ - + ]: 73506 : assert(blob != NULL);
9527 : 73506 : return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
9528 : : }
9529 : :
9530 : : bool
9531 : 82865 : spdk_blob_is_esnap_clone(const struct spdk_blob *blob)
9532 : : {
9533 : 82865 : return blob_is_esnap_clone(blob);
9534 : : }
9535 : :
9536 : : static void
9537 : 6948 : blob_update_clear_method(struct spdk_blob *blob)
9538 : : {
9539 : : enum blob_clear_method stored_cm;
9540 : :
9541 [ - + ]: 6948 : assert(blob != NULL);
9542 : :
9543 : : /* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored
9544 : : * in metadata previously. If something other than the default was
9545 : : * specified, ignore stored value and used what was passed in.
9546 : : */
9547 : 6948 : stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT);
9548 : :
9549 [ + - ]: 6948 : if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) {
9550 : 6948 : blob->clear_method = stored_cm;
9551 [ # # ]: 0 : } else if (blob->clear_method != stored_cm) {
9552 : 0 : SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n",
9553 : : blob->clear_method, stored_cm);
9554 : : }
9555 : 6948 : }
9556 : :
9557 : : spdk_blob_id
9558 : 520 : spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id)
9559 : : {
9560 : 520 : struct spdk_blob_list *snapshot_entry = NULL;
9561 : 520 : struct spdk_blob_list *clone_entry = NULL;
9562 : :
9563 [ + + ]: 992 : TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
9564 [ + + ]: 1470 : TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
9565 [ + + ]: 998 : if (clone_entry->id == blob_id) {
9566 : 340 : return snapshot_entry->id;
9567 : : }
9568 : : }
9569 : : }
9570 : :
9571 : 180 : return SPDK_BLOBID_INVALID;
9572 : : }
9573 : :
9574 : : int
9575 : 414 : spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids,
9576 : : size_t *count)
9577 : : {
9578 : : struct spdk_blob_list *snapshot_entry, *clone_entry;
9579 : : size_t n;
9580 : :
9581 : 414 : snapshot_entry = bs_get_snapshot_entry(bs, blobid);
9582 [ + + ]: 414 : if (snapshot_entry == NULL) {
9583 : 70 : *count = 0;
9584 : 70 : return 0;
9585 : : }
9586 : :
9587 [ + + + + ]: 344 : if (ids == NULL || *count < snapshot_entry->clone_count) {
9588 : 20 : *count = snapshot_entry->clone_count;
9589 : 20 : return -ENOMEM;
9590 : : }
9591 : 324 : *count = snapshot_entry->clone_count;
9592 : :
9593 : 324 : n = 0;
9594 [ + + ]: 688 : TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
9595 : 364 : ids[n++] = clone_entry->id;
9596 : : }
9597 : :
9598 : 324 : return 0;
9599 : : }
9600 : :
9601 : : static void
9602 : 8 : bs_load_grow_continue(struct spdk_bs_load_ctx *ctx)
9603 : : {
9604 : : int rc;
9605 : :
9606 [ - + ]: 8 : if (ctx->super->size == 0) {
9607 : 0 : ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9608 : : }
9609 : :
9610 [ - + ]: 8 : if (ctx->super->io_unit_size == 0) {
9611 : 0 : ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
9612 : : }
9613 : :
9614 : : /* Parse the super block */
9615 : 8 : ctx->bs->clean = 1;
9616 : 8 : ctx->bs->cluster_sz = ctx->super->cluster_size;
9617 : 8 : ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
9618 : 8 : ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
9619 [ + - ]: 8 : if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
9620 : 8 : ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
9621 : : }
9622 : 8 : ctx->bs->io_unit_size = ctx->super->io_unit_size;
9623 : 8 : rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
9624 [ - + ]: 8 : if (rc < 0) {
9625 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
9626 : 0 : return;
9627 : : }
9628 : 8 : ctx->bs->md_start = ctx->super->md_start;
9629 : 8 : ctx->bs->md_len = ctx->super->md_len;
9630 : 8 : rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
9631 [ - + ]: 8 : if (rc < 0) {
9632 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
9633 : 0 : return;
9634 : : }
9635 : :
9636 : 16 : ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
9637 : 8 : ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
9638 : 8 : ctx->bs->super_blob = ctx->super->super_blob;
9639 : 8 : memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
9640 : :
9641 [ + - - + ]: 8 : if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) {
9642 : 0 : SPDK_ERRLOG("Can not grow an unclean blobstore, please load it normally to clean it.\n");
9643 : 0 : bs_load_ctx_fail(ctx, -EIO);
9644 : 0 : return;
9645 : : } else {
9646 : 8 : bs_load_read_used_pages(ctx);
9647 : : }
9648 : : }
9649 : :
9650 : : static void
9651 : 8 : bs_load_grow_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9652 : : {
9653 : 8 : struct spdk_bs_load_ctx *ctx = cb_arg;
9654 : :
9655 [ - + ]: 8 : if (bserrno != 0) {
9656 : 0 : bs_load_ctx_fail(ctx, bserrno);
9657 : 0 : return;
9658 : : }
9659 : 8 : bs_load_grow_continue(ctx);
9660 : : }
9661 : :
9662 : : static void
9663 : 8 : bs_load_grow_used_clusters_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9664 : : {
9665 : 8 : struct spdk_bs_load_ctx *ctx = cb_arg;
9666 : :
9667 [ - + ]: 8 : if (bserrno != 0) {
9668 : 0 : bs_load_ctx_fail(ctx, bserrno);
9669 : 0 : return;
9670 : : }
9671 : :
9672 : 8 : spdk_free(ctx->mask);
9673 : :
9674 : 8 : bs_sequence_write_dev(ctx->seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
9675 : 8 : bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
9676 : : bs_load_grow_super_write_cpl, ctx);
9677 : : }
9678 : :
9679 : : static void
9680 : 8 : bs_load_grow_used_clusters_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9681 : : {
9682 : 8 : struct spdk_bs_load_ctx *ctx = cb_arg;
9683 : : uint64_t lba, lba_count;
9684 : : uint64_t dev_size;
9685 : : uint64_t total_clusters;
9686 : :
9687 [ - + ]: 8 : if (bserrno != 0) {
9688 : 0 : bs_load_ctx_fail(ctx, bserrno);
9689 : 0 : return;
9690 : : }
9691 : :
9692 : : /* The type must be correct */
9693 [ - + ]: 8 : assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
9694 : : /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
9695 [ - + ]: 8 : assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
9696 : : struct spdk_blob_md_page) * 8));
9697 : 8 : dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9698 : 8 : total_clusters = dev_size / ctx->super->cluster_size;
9699 : 8 : ctx->mask->length = total_clusters;
9700 : :
9701 : 8 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
9702 : 8 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
9703 : 8 : bs_sequence_write_dev(ctx->seq, ctx->mask, lba, lba_count,
9704 : : bs_load_grow_used_clusters_write_cpl, ctx);
9705 : : }
9706 : :
9707 : : static void
9708 : 8 : bs_load_try_to_grow(struct spdk_bs_load_ctx *ctx)
9709 : : {
9710 : : uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
9711 : : uint64_t lba, lba_count, mask_size;
9712 : :
9713 : 8 : dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9714 : 8 : total_clusters = dev_size / ctx->super->cluster_size;
9715 : 8 : used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
9716 : 8 : spdk_divide_round_up(total_clusters, 8),
9717 : : SPDK_BS_PAGE_SIZE);
9718 : 8 : max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
9719 : : /* No necessary to grow or no space to grow */
9720 [ + - - + ]: 8 : if (ctx->super->size >= dev_size || used_cluster_mask_len > max_used_cluster_mask) {
9721 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "No grow\n");
9722 : 0 : bs_load_grow_continue(ctx);
9723 : 0 : return;
9724 : : }
9725 : :
9726 [ - + ]: 8 : SPDK_DEBUGLOG(blob, "Resize blobstore\n");
9727 : :
9728 : 8 : ctx->super->size = dev_size;
9729 : 8 : ctx->super->used_cluster_mask_len = used_cluster_mask_len;
9730 : 8 : ctx->super->crc = blob_md_page_calc_crc(ctx->super);
9731 : :
9732 : 8 : mask_size = used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
9733 : 8 : ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
9734 : : SPDK_MALLOC_DMA);
9735 [ - + ]: 8 : if (!ctx->mask) {
9736 : 0 : bs_load_ctx_fail(ctx, -ENOMEM);
9737 : 0 : return;
9738 : : }
9739 : 8 : lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
9740 : 8 : lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
9741 : 8 : bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
9742 : : bs_load_grow_used_clusters_read_cpl, ctx);
9743 : : }
9744 : :
9745 : : static void
9746 : 8 : bs_grow_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9747 : : {
9748 : 8 : struct spdk_bs_load_ctx *ctx = cb_arg;
9749 : : int rc;
9750 : :
9751 : 8 : rc = bs_super_validate(ctx->super, ctx->bs);
9752 [ - + ]: 8 : if (rc != 0) {
9753 : 0 : bs_load_ctx_fail(ctx, rc);
9754 : 0 : return;
9755 : : }
9756 : :
9757 : 8 : bs_load_try_to_grow(ctx);
9758 : : }
9759 : :
9760 : : struct spdk_bs_grow_ctx {
9761 : : struct spdk_blob_store *bs;
9762 : : struct spdk_bs_super_block *super;
9763 : :
9764 : : struct spdk_bit_pool *new_used_clusters;
9765 : : struct spdk_bs_md_mask *new_used_clusters_mask;
9766 : :
9767 : : spdk_bs_sequence_t *seq;
9768 : : };
9769 : :
9770 : : static void
9771 : 64 : bs_grow_live_done(struct spdk_bs_grow_ctx *ctx, int bserrno)
9772 : : {
9773 [ + + ]: 64 : if (bserrno != 0) {
9774 : 16 : spdk_bit_pool_free(&ctx->new_used_clusters);
9775 : : }
9776 : :
9777 : 64 : bs_sequence_finish(ctx->seq, bserrno);
9778 : 64 : free(ctx->new_used_clusters_mask);
9779 : 64 : spdk_free(ctx->super);
9780 : 64 : free(ctx);
9781 : 64 : }
9782 : :
9783 : : static void
9784 : 16 : bs_grow_live_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9785 : : {
9786 : 16 : struct spdk_bs_grow_ctx *ctx = cb_arg;
9787 : 16 : struct spdk_blob_store *bs = ctx->bs;
9788 : : uint64_t total_clusters;
9789 : :
9790 [ - + ]: 16 : if (bserrno != 0) {
9791 : 0 : bs_grow_live_done(ctx, bserrno);
9792 : 0 : return;
9793 : : }
9794 : :
9795 : : /*
9796 : : * Blobstore is not clean until unload, for now only the super block is up to date.
9797 : : * This is similar to state right after blobstore init, when bs_write_used_md() didn't
9798 : : * yet execute.
9799 : : * When cleanly unloaded, the used md pages will be written out.
9800 : : * In case of unclean shutdown, loading blobstore will go through recovery path correctly
9801 : : * filling out the used_clusters with new size and writing it out.
9802 : : */
9803 : 16 : bs->clean = 0;
9804 : :
9805 : : /* Reverting the super->size past this point is complex, avoid any error paths
9806 : : * that require to do so. */
9807 : 16 : spdk_spin_lock(&bs->used_lock);
9808 : :
9809 : 16 : total_clusters = ctx->super->size / ctx->super->cluster_size;
9810 : :
9811 [ - + ]: 16 : assert(total_clusters >= spdk_bit_pool_capacity(bs->used_clusters));
9812 : 16 : spdk_bit_pool_store_mask(bs->used_clusters, ctx->new_used_clusters_mask);
9813 : :
9814 [ - + ]: 16 : assert(total_clusters == spdk_bit_pool_capacity(ctx->new_used_clusters));
9815 : 16 : spdk_bit_pool_load_mask(ctx->new_used_clusters, ctx->new_used_clusters_mask);
9816 : :
9817 : 16 : spdk_bit_pool_free(&bs->used_clusters);
9818 : 16 : bs->used_clusters = ctx->new_used_clusters;
9819 : :
9820 : 16 : bs->total_clusters = total_clusters;
9821 : 32 : bs->total_data_clusters = bs->total_clusters - spdk_divide_round_up(
9822 : 16 : bs->md_start + bs->md_len, bs->pages_per_cluster);
9823 : :
9824 : 16 : bs->num_free_clusters = spdk_bit_pool_count_free(bs->used_clusters);
9825 [ - + ]: 16 : assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
9826 : 16 : spdk_spin_unlock(&bs->used_lock);
9827 : :
9828 : 16 : bs_grow_live_done(ctx, 0);
9829 : : }
9830 : :
9831 : : static void
9832 : 64 : bs_grow_live_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
9833 : : {
9834 : 64 : struct spdk_bs_grow_ctx *ctx = cb_arg;
9835 : : uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
9836 : : int rc;
9837 : :
9838 [ - + ]: 64 : if (bserrno != 0) {
9839 : 0 : bs_grow_live_done(ctx, bserrno);
9840 : 0 : return;
9841 : : }
9842 : :
9843 : 64 : rc = bs_super_validate(ctx->super, ctx->bs);
9844 [ + + ]: 64 : if (rc != 0) {
9845 : 8 : bs_grow_live_done(ctx, rc);
9846 : 8 : return;
9847 : : }
9848 : :
9849 : 56 : dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
9850 : 56 : total_clusters = dev_size / ctx->super->cluster_size;
9851 : 56 : used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
9852 : 56 : spdk_divide_round_up(total_clusters, 8),
9853 : : SPDK_BS_PAGE_SIZE);
9854 : 56 : max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
9855 : : /* Only checking dev_size. Since it can change, but total_clusters remain the same. */
9856 [ + + ]: 56 : if (dev_size == ctx->super->size) {
9857 [ - + ]: 32 : SPDK_DEBUGLOG(blob, "No need to grow blobstore\n");
9858 : 32 : bs_grow_live_done(ctx, 0);
9859 : 32 : return;
9860 : : }
9861 : : /*
9862 : : * Blobstore cannot be shrunk, so check before if:
9863 : : * - new size of the device is smaller than size in super_block
9864 : : * - new total number of clusters is smaller than used_clusters bit_pool
9865 : : * - there is enough space in metadata for used_cluster_mask to be written out
9866 : : */
9867 [ + - ]: 24 : if (dev_size < ctx->super->size ||
9868 [ + - + + ]: 24 : total_clusters < spdk_bit_pool_capacity(ctx->bs->used_clusters) ||
9869 : : used_cluster_mask_len > max_used_cluster_mask) {
9870 [ - + ]: 8 : SPDK_DEBUGLOG(blob, "No space to grow blobstore\n");
9871 : 8 : bs_grow_live_done(ctx, -ENOSPC);
9872 : 8 : return;
9873 : : }
9874 : :
9875 [ - + ]: 16 : SPDK_DEBUGLOG(blob, "Resizing blobstore\n");
9876 : :
9877 : 16 : ctx->new_used_clusters_mask = calloc(1, total_clusters);
9878 [ - + ]: 16 : if (!ctx->new_used_clusters_mask) {
9879 : 0 : bs_grow_live_done(ctx, -ENOMEM);
9880 : 0 : return;
9881 : : }
9882 : 16 : ctx->new_used_clusters = spdk_bit_pool_create(total_clusters);
9883 [ - + ]: 16 : if (!ctx->new_used_clusters) {
9884 : 0 : bs_grow_live_done(ctx, -ENOMEM);
9885 : 0 : return;
9886 : : }
9887 : :
9888 : 16 : ctx->super->clean = 0;
9889 : 16 : ctx->super->size = dev_size;
9890 : 16 : ctx->super->used_cluster_mask_len = used_cluster_mask_len;
9891 : 16 : bs_write_super(seq, ctx->bs, ctx->super, bs_grow_live_super_write_cpl, ctx);
9892 : : }
9893 : :
9894 : : void
9895 : 64 : spdk_bs_grow_live(struct spdk_blob_store *bs,
9896 : : spdk_bs_op_complete cb_fn, void *cb_arg)
9897 : : {
9898 : 64 : struct spdk_bs_cpl cpl;
9899 : : struct spdk_bs_grow_ctx *ctx;
9900 : :
9901 [ - + ]: 64 : assert(spdk_get_thread() == bs->md_thread);
9902 : :
9903 [ - + ]: 64 : SPDK_DEBUGLOG(blob, "Growing blobstore on dev %p\n", bs->dev);
9904 : :
9905 : 64 : cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
9906 : 64 : cpl.u.bs_basic.cb_fn = cb_fn;
9907 : 64 : cpl.u.bs_basic.cb_arg = cb_arg;
9908 : :
9909 : 64 : ctx = calloc(1, sizeof(struct spdk_bs_grow_ctx));
9910 [ - + ]: 64 : if (!ctx) {
9911 : 0 : cb_fn(cb_arg, -ENOMEM);
9912 : 0 : return;
9913 : : }
9914 : 64 : ctx->bs = bs;
9915 : :
9916 : 64 : ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
9917 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
9918 [ - + ]: 64 : if (!ctx->super) {
9919 : 0 : free(ctx);
9920 : 0 : cb_fn(cb_arg, -ENOMEM);
9921 : 0 : return;
9922 : : }
9923 : :
9924 : 64 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
9925 [ - + ]: 64 : if (!ctx->seq) {
9926 : 0 : spdk_free(ctx->super);
9927 : 0 : free(ctx);
9928 : 0 : cb_fn(cb_arg, -ENOMEM);
9929 : 0 : return;
9930 : : }
9931 : :
9932 : : /* Read the super block */
9933 : 64 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
9934 : 64 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
9935 : : bs_grow_live_load_super_cpl, ctx);
9936 : : }
9937 : :
9938 : : void
9939 : 8 : spdk_bs_grow(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
9940 : : spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
9941 : : {
9942 : 8 : struct spdk_blob_store *bs;
9943 : 8 : struct spdk_bs_cpl cpl;
9944 : 8 : struct spdk_bs_load_ctx *ctx;
9945 : 8 : struct spdk_bs_opts opts = {};
9946 : : int err;
9947 : :
9948 [ - + ]: 8 : SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
9949 : :
9950 [ - + ]: 8 : if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
9951 [ # # ]: 0 : SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
9952 : 0 : dev->destroy(dev);
9953 : 0 : cb_fn(cb_arg, NULL, -EINVAL);
9954 : 0 : return;
9955 : : }
9956 : :
9957 : 8 : spdk_bs_opts_init(&opts, sizeof(opts));
9958 [ + - ]: 8 : if (o) {
9959 [ - + ]: 8 : if (bs_opts_copy(o, &opts)) {
9960 : 0 : return;
9961 : : }
9962 : : }
9963 : :
9964 [ + - - + ]: 8 : if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
9965 : 0 : dev->destroy(dev);
9966 : 0 : cb_fn(cb_arg, NULL, -EINVAL);
9967 : 0 : return;
9968 : : }
9969 : :
9970 : 8 : err = bs_alloc(dev, &opts, &bs, &ctx);
9971 [ - + ]: 8 : if (err) {
9972 : 0 : dev->destroy(dev);
9973 : 0 : cb_fn(cb_arg, NULL, err);
9974 : 0 : return;
9975 : : }
9976 : :
9977 : 8 : cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
9978 : 8 : cpl.u.bs_handle.cb_fn = cb_fn;
9979 : 8 : cpl.u.bs_handle.cb_arg = cb_arg;
9980 : 8 : cpl.u.bs_handle.bs = bs;
9981 : :
9982 : 8 : ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
9983 [ - + ]: 8 : if (!ctx->seq) {
9984 : 0 : spdk_free(ctx->super);
9985 : 0 : free(ctx);
9986 : 0 : bs_free(bs);
9987 : 0 : cb_fn(cb_arg, NULL, -ENOMEM);
9988 : 0 : return;
9989 : : }
9990 : :
9991 : : /* Read the super block */
9992 : 8 : bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
9993 : 8 : bs_byte_to_lba(bs, sizeof(*ctx->super)),
9994 : : bs_grow_load_super_cpl, ctx);
9995 : : }
9996 : :
9997 : : int
9998 : 48 : spdk_blob_get_esnap_id(struct spdk_blob *blob, const void **id, size_t *len)
9999 : : {
10000 [ + + ]: 48 : if (!blob_is_esnap_clone(blob)) {
10001 : 24 : return -EINVAL;
10002 : : }
10003 : :
10004 : 24 : return blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, id, len, true);
10005 : : }
10006 : :
10007 : : struct spdk_io_channel *
10008 : 17680 : blob_esnap_get_io_channel(struct spdk_io_channel *ch, struct spdk_blob *blob)
10009 : : {
10010 : 17680 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(ch);
10011 : 17680 : struct spdk_bs_dev *bs_dev = blob->back_bs_dev;
10012 : 17680 : struct blob_esnap_channel find = {};
10013 : : struct blob_esnap_channel *esnap_channel, *existing;
10014 : :
10015 : 17680 : find.blob_id = blob->id;
10016 : 17680 : esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
10017 [ + + ]: 17680 : if (spdk_likely(esnap_channel != NULL)) {
10018 [ - + ]: 17592 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": using cached channel on thread %s\n",
10019 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
10020 : 17592 : return esnap_channel->channel;
10021 : : }
10022 : :
10023 [ - + ]: 88 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": allocating channel on thread %s\n",
10024 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
10025 : :
10026 : 88 : esnap_channel = calloc(1, sizeof(*esnap_channel));
10027 [ - + ]: 88 : if (esnap_channel == NULL) {
10028 : 0 : SPDK_NOTICELOG("blob 0x%" PRIx64 " channel allocation failed: no memory\n",
10029 : : find.blob_id);
10030 : 0 : return NULL;
10031 : : }
10032 : 88 : esnap_channel->channel = bs_dev->create_channel(bs_dev);
10033 [ - + ]: 88 : if (esnap_channel->channel == NULL) {
10034 : 0 : SPDK_NOTICELOG("blob 0x%" PRIx64 " back channel allocation failed\n", blob->id);
10035 : 0 : free(esnap_channel);
10036 : 0 : return NULL;
10037 : : }
10038 : 88 : esnap_channel->blob_id = find.blob_id;
10039 : 88 : existing = RB_INSERT(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
10040 [ - + ]: 88 : if (spdk_unlikely(existing != NULL)) {
10041 : : /*
10042 : : * This should be unreachable: all modifications to this tree happen on this thread.
10043 : : */
10044 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 "lost race to allocate a channel\n", find.blob_id);
10045 : 0 : assert(false);
10046 : :
10047 : : bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
10048 : : free(esnap_channel);
10049 : :
10050 : : return existing->channel;
10051 : : }
10052 : :
10053 : 88 : return esnap_channel->channel;
10054 : : }
10055 : :
10056 : : static int
10057 : 17632 : blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2)
10058 : : {
10059 [ + - ]: 17632 : return (c1->blob_id < c2->blob_id ? -1 : c1->blob_id > c2->blob_id);
10060 : : }
10061 : :
10062 : : struct blob_esnap_destroy_ctx {
10063 : : spdk_blob_op_with_handle_complete cb_fn;
10064 : : void *cb_arg;
10065 : : struct spdk_blob *blob;
10066 : : struct spdk_bs_dev *back_bs_dev;
10067 : : bool abort_io;
10068 : : };
10069 : :
10070 : : static void
10071 : 272 : blob_esnap_destroy_channels_done(struct spdk_io_channel_iter *i, int status)
10072 : : {
10073 : 272 : struct blob_esnap_destroy_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
10074 : 272 : struct spdk_blob *blob = ctx->blob;
10075 : 272 : struct spdk_blob_store *bs = blob->bs;
10076 : :
10077 [ - + ]: 272 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": done destroying channels for this blob\n",
10078 : : blob->id);
10079 : :
10080 [ + + ]: 272 : if (ctx->cb_fn != NULL) {
10081 : 248 : ctx->cb_fn(ctx->cb_arg, blob, status);
10082 : : }
10083 : 272 : free(ctx);
10084 : :
10085 : 272 : bs->esnap_channels_unloading--;
10086 [ + - + + ]: 272 : if (bs->esnap_channels_unloading == 0 && bs->esnap_unload_cb_fn != NULL) {
10087 : 8 : spdk_bs_unload(bs, bs->esnap_unload_cb_fn, bs->esnap_unload_cb_arg);
10088 : : }
10089 : 272 : }
10090 : :
10091 : : static void
10092 : 288 : blob_esnap_destroy_one_channel(struct spdk_io_channel_iter *i)
10093 : : {
10094 : 288 : struct blob_esnap_destroy_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
10095 : 288 : struct spdk_blob *blob = ctx->blob;
10096 : 288 : struct spdk_bs_dev *bs_dev = ctx->back_bs_dev;
10097 : 288 : struct spdk_io_channel *channel = spdk_io_channel_iter_get_channel(i);
10098 : 288 : struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(channel);
10099 : : struct blob_esnap_channel *esnap_channel;
10100 : 288 : struct blob_esnap_channel find = {};
10101 : :
10102 [ - + ]: 288 : assert(spdk_get_thread() == spdk_io_channel_get_thread(channel));
10103 : :
10104 : 288 : find.blob_id = blob->id;
10105 : 288 : esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
10106 [ + + ]: 288 : if (esnap_channel != NULL) {
10107 [ - + ]: 24 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channel on thread %s\n",
10108 : : blob->id, spdk_thread_get_name(spdk_get_thread()));
10109 : 24 : RB_REMOVE(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
10110 : :
10111 [ + + ]: 24 : if (ctx->abort_io) {
10112 : : spdk_bs_user_op_t *op, *tmp;
10113 : :
10114 [ - + ]: 16 : TAILQ_FOREACH_SAFE(op, &bs_channel->queued_io, link, tmp) {
10115 [ # # ]: 0 : if (op->back_channel == esnap_channel->channel) {
10116 [ # # ]: 0 : TAILQ_REMOVE(&bs_channel->queued_io, op, link);
10117 : 0 : bs_user_op_abort(op, -EIO);
10118 : : }
10119 : : }
10120 : : }
10121 : :
10122 : 24 : bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
10123 : 24 : free(esnap_channel);
10124 : : }
10125 : :
10126 : 288 : spdk_for_each_channel_continue(i, 0);
10127 : 288 : }
10128 : :
10129 : : /*
10130 : : * Destroy the channels for a specific blob on each thread with a blobstore channel. This should be
10131 : : * used when closing an esnap clone blob and after decoupling from the parent.
10132 : : */
10133 : : static void
10134 : 962 : blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
10135 : : spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
10136 : : {
10137 : : struct blob_esnap_destroy_ctx *ctx;
10138 : :
10139 [ + + + + ]: 962 : if (!blob_is_esnap_clone(blob) || blob->back_bs_dev == NULL) {
10140 [ + - ]: 690 : if (cb_fn != NULL) {
10141 : 690 : cb_fn(cb_arg, blob, 0);
10142 : : }
10143 : 690 : return;
10144 : : }
10145 : :
10146 : 272 : ctx = calloc(1, sizeof(*ctx));
10147 [ - + ]: 272 : if (ctx == NULL) {
10148 [ # # ]: 0 : if (cb_fn != NULL) {
10149 : 0 : cb_fn(cb_arg, blob, -ENOMEM);
10150 : : }
10151 : 0 : return;
10152 : : }
10153 : 272 : ctx->cb_fn = cb_fn;
10154 : 272 : ctx->cb_arg = cb_arg;
10155 : 272 : ctx->blob = blob;
10156 : 272 : ctx->back_bs_dev = blob->back_bs_dev;
10157 : 272 : ctx->abort_io = abort_io;
10158 : :
10159 [ - + ]: 272 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channels for this blob\n",
10160 : : blob->id);
10161 : :
10162 : 272 : blob->bs->esnap_channels_unloading++;
10163 : 272 : spdk_for_each_channel(blob->bs, blob_esnap_destroy_one_channel, ctx,
10164 : : blob_esnap_destroy_channels_done);
10165 : : }
10166 : :
10167 : : /*
10168 : : * Destroy all bs_dev channels on a specific blobstore channel. This should be used when a
10169 : : * bs_channel is destroyed.
10170 : : */
10171 : : static void
10172 : 3803 : blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch)
10173 : : {
10174 : : struct blob_esnap_channel *esnap_channel, *esnap_channel_tmp;
10175 : :
10176 [ - + ]: 3803 : assert(spdk_get_thread() == spdk_io_channel_get_thread(spdk_io_channel_from_ctx(ch)));
10177 : :
10178 [ - + ]: 3803 : SPDK_DEBUGLOG(blob_esnap, "destroying channels on thread %s\n",
10179 : : spdk_thread_get_name(spdk_get_thread()));
10180 [ + + + - ]: 3867 : RB_FOREACH_SAFE(esnap_channel, blob_esnap_channel_tree, &ch->esnap_channels,
10181 : : esnap_channel_tmp) {
10182 [ - + ]: 64 : SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64
10183 : : ": destroying one channel in thread %s\n",
10184 : : esnap_channel->blob_id, spdk_thread_get_name(spdk_get_thread()));
10185 : 64 : RB_REMOVE(blob_esnap_channel_tree, &ch->esnap_channels, esnap_channel);
10186 : 64 : spdk_put_io_channel(esnap_channel->channel);
10187 : 64 : free(esnap_channel);
10188 : : }
10189 [ - + ]: 3803 : SPDK_DEBUGLOG(blob_esnap, "done destroying channels on thread %s\n",
10190 : : spdk_thread_get_name(spdk_get_thread()));
10191 : 3803 : }
10192 : :
10193 : : static void
10194 : 56 : blob_set_back_bs_dev_done(void *_ctx, int bserrno)
10195 : : {
10196 : 56 : struct set_bs_dev_ctx *ctx = _ctx;
10197 : :
10198 [ - + ]: 56 : if (bserrno != 0) {
10199 : : /* Even though the unfreeze failed, the update may have succeed. */
10200 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": unfreeze failed with error %d\n", ctx->blob->id,
10201 : : bserrno);
10202 : : }
10203 : 56 : ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
10204 : 56 : free(ctx);
10205 : 56 : }
10206 : :
10207 : : static void
10208 : 56 : blob_frozen_set_back_bs_dev(void *_ctx, struct spdk_blob *blob, int bserrno)
10209 : : {
10210 : 56 : struct set_bs_dev_ctx *ctx = _ctx;
10211 : : int rc;
10212 : :
10213 [ - + ]: 56 : if (bserrno != 0) {
10214 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to release old back_bs_dev with error %d\n",
10215 : : blob->id, bserrno);
10216 : 0 : ctx->bserrno = bserrno;
10217 : 0 : blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
10218 : 0 : return;
10219 : : }
10220 : :
10221 [ + - ]: 56 : if (blob->back_bs_dev != NULL) {
10222 : 56 : blob->back_bs_dev->destroy(blob->back_bs_dev);
10223 : 56 : blob->back_bs_dev = NULL;
10224 : : }
10225 : :
10226 [ + + ]: 56 : if (ctx->parent_refs_cb_fn) {
10227 : 40 : rc = ctx->parent_refs_cb_fn(blob, ctx->parent_refs_cb_arg);
10228 [ - + ]: 40 : if (rc != 0) {
10229 : 0 : ctx->bserrno = rc;
10230 : 0 : blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
10231 : 0 : return;
10232 : : }
10233 : : }
10234 : :
10235 : 56 : SPDK_NOTICELOG("blob 0x%" PRIx64 ": hotplugged back_bs_dev\n", blob->id);
10236 : 56 : blob->back_bs_dev = ctx->back_bs_dev;
10237 : 56 : ctx->bserrno = 0;
10238 : :
10239 : 56 : blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
10240 : : }
10241 : :
10242 : : static void
10243 : 56 : blob_set_back_bs_dev_frozen(void *_ctx, int bserrno)
10244 : : {
10245 : 56 : struct set_bs_dev_ctx *ctx = _ctx;
10246 : 56 : struct spdk_blob *blob = ctx->blob;
10247 : :
10248 [ - + ]: 56 : if (bserrno != 0) {
10249 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to freeze with error %d\n", blob->id,
10250 : : bserrno);
10251 : 0 : ctx->cb_fn(ctx->cb_arg, bserrno);
10252 : 0 : free(ctx);
10253 : 0 : return;
10254 : : }
10255 : :
10256 : : /*
10257 : : * This does not prevent future reads from the esnap device because any future IO will
10258 : : * lazily create a new esnap IO channel.
10259 : : */
10260 : 56 : blob_esnap_destroy_bs_dev_channels(blob, true, blob_frozen_set_back_bs_dev, ctx);
10261 : : }
10262 : :
10263 : : void
10264 : 16 : spdk_blob_set_esnap_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
10265 : : spdk_blob_op_complete cb_fn, void *cb_arg)
10266 : : {
10267 [ - + ]: 16 : if (!blob_is_esnap_clone(blob)) {
10268 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
10269 : 0 : cb_fn(cb_arg, -EINVAL);
10270 : 0 : return;
10271 : : }
10272 : :
10273 : 16 : blob_set_back_bs_dev(blob, back_bs_dev, NULL, NULL, cb_fn, cb_arg);
10274 : : }
10275 : :
10276 : : struct spdk_bs_dev *
10277 : 8 : spdk_blob_get_esnap_bs_dev(const struct spdk_blob *blob)
10278 : : {
10279 [ - + ]: 8 : if (!blob_is_esnap_clone(blob)) {
10280 : 0 : SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
10281 : 0 : return NULL;
10282 : : }
10283 : :
10284 : 8 : return blob->back_bs_dev;
10285 : : }
10286 : :
10287 : : bool
10288 : 99 : spdk_blob_is_degraded(const struct spdk_blob *blob)
10289 : : {
10290 [ + + + + ]: 99 : if (blob->bs->dev->is_degraded != NULL && blob->bs->dev->is_degraded(blob->bs->dev)) {
10291 : 8 : return true;
10292 : : }
10293 [ + + + + ]: 91 : if (blob->back_bs_dev == NULL || blob->back_bs_dev->is_degraded == NULL) {
10294 : 57 : return false;
10295 : : }
10296 : :
10297 : 34 : return blob->back_bs_dev->is_degraded(blob->back_bs_dev);
10298 : : }
10299 : :
10300 : 493 : SPDK_LOG_REGISTER_COMPONENT(blob)
10301 : 493 : SPDK_LOG_REGISTER_COMPONENT(blob_esnap)
10302 : :
10303 : :
10304 : 945 : SPDK_TRACE_REGISTER_FN(blob_trace, "blob", TRACE_GROUP_BLOB)
10305 : : {
10306 : 452 : struct spdk_trace_tpoint_opts opts[] = {
10307 : : {
10308 : : "BLOB_PROCESS_START", TRACE_BLOB_PROCESS_START,
10309 : : OWNER_TYPE_NONE, OBJECT_BLOB_CB_ARG, 1,
10310 : : {
10311 : : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }
10312 : : }
10313 : : },
10314 : : {
10315 : : "BLOB_PROCESS_COMPLETE", TRACE_BLOB_PROCESS_COMPLETE,
10316 : : OWNER_TYPE_NONE, OBJECT_BLOB_CB_ARG, 0,
10317 : : {
10318 : : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }
10319 : : }
10320 : : },
10321 : : };
10322 : :
10323 : :
10324 : 452 : spdk_trace_register_object(OBJECT_BLOB_CB_ARG, 'a');
10325 : 452 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
10326 : 452 : spdk_trace_tpoint_register_relation(TRACE_BDEV_IO_START, OBJECT_BLOB_CB_ARG, 1);
10327 : 452 : spdk_trace_tpoint_register_relation(TRACE_BDEV_IO_DONE, OBJECT_BLOB_CB_ARG, 0);
10328 : 452 : }
|