Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2017 Intel Corporation.
3 : * All rights reserved.
4 : * Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : */
6 :
7 : #ifndef SPDK_BLOBSTORE_H
8 : #define SPDK_BLOBSTORE_H
9 :
10 : #include "spdk/assert.h"
11 : #include "spdk/blob.h"
12 : #include "spdk/queue.h"
13 : #include "spdk/util.h"
14 : #include "spdk/tree.h"
15 : #include "spdk/thread.h"
16 :
17 : #include "request.h"
18 :
19 : /* In Memory Data Structures
20 : *
21 : * The following data structures exist only in memory.
22 : */
23 :
24 : #define SPDK_BLOB_OPTS_CLUSTER_SZ (1024 * 1024)
25 : #define SPDK_BLOB_OPTS_NUM_MD_PAGES UINT32_MAX
26 : #define SPDK_BLOB_OPTS_MAX_MD_OPS 32
27 : #define SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS 512
28 : #define SPDK_BLOB_BLOBID_HIGH_BIT (1ULL << 32)
29 :
30 : struct spdk_xattr {
31 : uint32_t index;
32 : uint16_t value_len;
33 : char *name;
34 : void *value;
35 : TAILQ_ENTRY(spdk_xattr) link;
36 : };
37 :
38 : /* The mutable part of the blob data that is sync'd to
39 : * disk. The data in here is both mutable and persistent.
40 : */
41 : struct spdk_blob_mut_data {
42 : /* Number of data clusters in the blob */
43 : uint64_t num_clusters;
44 :
45 : /* Array LBAs that are the beginning of a cluster, in
46 : * the order they appear in the blob.
47 : */
48 : uint64_t *clusters;
49 :
50 : /* The size of the clusters array. This is greater than or
51 : * equal to 'num_clusters'.
52 : */
53 : size_t cluster_array_size;
54 :
55 : /* The number of allocated clusters in the clusters array */
56 : uint64_t num_allocated_clusters;
57 :
58 : /* Number of extent pages */
59 : uint64_t num_extent_pages;
60 :
61 : /* Array of page offsets into the metadata region,
62 : * containing extents. Can contain entries for not yet
63 : * allocated pages. */
64 : uint32_t *extent_pages;
65 :
66 : /* The size of the extent page array. This is greater than or
67 : * equal to 'num_extent_pages'. */
68 : size_t extent_pages_array_size;
69 :
70 : /* Number of metadata pages */
71 : uint32_t num_pages;
72 :
73 : /* Array of page offsets into the metadata region, in
74 : * the order of the metadata page sequence.
75 : */
76 : uint32_t *pages;
77 : };
78 :
79 : enum spdk_blob_state {
80 : /* The blob in-memory version does not match the on-disk
81 : * version.
82 : */
83 : SPDK_BLOB_STATE_DIRTY,
84 :
85 : /* The blob in memory version of the blob matches the on disk
86 : * version.
87 : */
88 : SPDK_BLOB_STATE_CLEAN,
89 :
90 : /* The in-memory state being synchronized with the on-disk
91 : * blob state. */
92 : SPDK_BLOB_STATE_LOADING,
93 : };
94 :
95 : TAILQ_HEAD(spdk_xattr_tailq, spdk_xattr);
96 :
97 : struct spdk_blob_list {
98 : spdk_blob_id id;
99 : size_t clone_count;
100 : TAILQ_HEAD(, spdk_blob_list) clones;
101 : TAILQ_ENTRY(spdk_blob_list) link;
102 : };
103 :
104 : struct spdk_blob {
105 : struct spdk_blob_store *bs;
106 :
107 : uint32_t open_ref;
108 :
109 : spdk_blob_id id;
110 : spdk_blob_id parent_id;
111 :
112 : enum spdk_blob_state state;
113 :
114 : /* Two copies of the mutable data. One is a version
115 : * that matches the last known data on disk (clean).
116 : * The other (active) is the current data. Syncing
117 : * a blob makes the clean match the active.
118 : */
119 : struct spdk_blob_mut_data clean;
120 : struct spdk_blob_mut_data active;
121 :
122 : bool invalid;
123 : bool data_ro;
124 : bool md_ro;
125 :
126 : uint64_t invalid_flags;
127 : uint64_t data_ro_flags;
128 : uint64_t md_ro_flags;
129 :
130 : struct spdk_bs_dev *back_bs_dev;
131 :
132 : /* When deparenting child of an esnap clone, two blobs
133 : * can share the same back_bs_dev. So, we need to keep
134 : * track of shared references.
135 : */
136 : LIST_ENTRY(spdk_blob) back_bs_dev_link;
137 :
138 : /* TODO: The xattrs are mutable, but we don't want to be
139 : * copying them unnecessarily. Figure this out.
140 : */
141 : struct spdk_xattr_tailq xattrs;
142 : struct spdk_xattr_tailq xattrs_internal;
143 :
144 : RB_ENTRY(spdk_blob) link;
145 :
146 : uint32_t frozen_refcnt;
147 : bool locked_operation_in_progress;
148 : enum blob_clear_method clear_method;
149 : bool extent_rle_found;
150 : bool extent_table_found;
151 : bool use_extent_table;
152 :
153 : /* A list of pending metadata pending_persists */
154 : TAILQ_HEAD(, spdk_blob_persist_ctx) pending_persists;
155 : TAILQ_HEAD(, spdk_blob_persist_ctx) persists_to_complete;
156 :
157 : /* Number of data clusters retrieved from extent table,
158 : * that many have to be read from extent pages. */
159 : uint64_t remaining_clusters_in_et;
160 : };
161 :
162 : struct spdk_blob_store {
163 : uint64_t md_start; /* Offset from beginning of disk, in pages */
164 : uint32_t md_len; /* Count, in pages */
165 :
166 : struct spdk_io_channel *md_channel;
167 : uint32_t max_channel_ops;
168 :
169 : struct spdk_thread *md_thread;
170 :
171 : struct spdk_bs_dev *dev;
172 :
173 : struct spdk_bit_array *used_md_pages; /* Protected by used_lock */
174 : struct spdk_bit_pool *used_clusters; /* Protected by used_lock */
175 : struct spdk_bit_array *used_blobids;
176 : struct spdk_bit_array *open_blobids;
177 :
178 : struct spdk_spinlock used_lock;
179 :
180 : uint32_t cluster_sz;
181 : uint64_t total_clusters;
182 : uint64_t total_data_clusters;
183 : uint64_t num_free_clusters; /* Protected by used_lock */
184 : uint64_t pages_per_cluster;
185 : uint8_t pages_per_cluster_shift;
186 : uint32_t io_unit_size;
187 :
188 : spdk_blob_id super_blob;
189 : struct spdk_bs_type bstype;
190 :
191 : struct spdk_bs_cpl unload_cpl;
192 : int unload_err;
193 :
194 : RB_HEAD(spdk_blob_tree, spdk_blob) open_blobs;
195 : TAILQ_HEAD(, spdk_blob_list) snapshots;
196 :
197 : bool clean;
198 :
199 : spdk_bs_esnap_dev_create esnap_bs_dev_create;
200 : void *esnap_ctx;
201 :
202 : /* If external snapshot channels are being destroyed while
203 : * the blobstore is unloaded, the unload is deferred until
204 : * after the channel destruction completes.
205 : */
206 : uint32_t esnap_channels_unloading;
207 : spdk_bs_op_complete esnap_unload_cb_fn;
208 : void *esnap_unload_cb_arg;
209 : };
210 :
211 : struct spdk_bs_channel {
212 : struct spdk_bs_request_set *req_mem;
213 : TAILQ_HEAD(, spdk_bs_request_set) reqs;
214 :
215 : struct spdk_blob_store *bs;
216 :
217 : struct spdk_bs_dev *dev;
218 : struct spdk_io_channel *dev_channel;
219 :
220 : /* This page is only used during insert of a new cluster. */
221 : struct spdk_blob_md_page *new_cluster_page;
222 :
223 : TAILQ_HEAD(, spdk_bs_request_set) need_cluster_alloc;
224 : TAILQ_HEAD(, spdk_bs_request_set) queued_io;
225 :
226 : RB_HEAD(blob_esnap_channel_tree, blob_esnap_channel) esnap_channels;
227 : };
228 :
229 : /** operation type */
230 : enum spdk_blob_op_type {
231 : SPDK_BLOB_WRITE,
232 : SPDK_BLOB_READ,
233 : SPDK_BLOB_UNMAP,
234 : SPDK_BLOB_WRITE_ZEROES,
235 : SPDK_BLOB_WRITEV,
236 : SPDK_BLOB_READV,
237 : };
238 :
239 : /* back bs_dev */
240 :
241 : #define BLOB_SNAPSHOT "SNAP"
242 : #define SNAPSHOT_IN_PROGRESS "SNAPTMP"
243 : #define SNAPSHOT_PENDING_REMOVAL "SNAPRM"
244 : #define BLOB_EXTERNAL_SNAPSHOT_ID "EXTSNAP"
245 :
246 : struct spdk_blob_bs_dev {
247 : struct spdk_bs_dev bs_dev;
248 : struct spdk_blob *blob;
249 : };
250 :
251 : /* On-Disk Data Structures
252 : *
253 : * The following data structures exist on disk.
254 : */
255 : #define SPDK_BS_INITIAL_VERSION 1
256 : #define SPDK_BS_VERSION 3 /* current version */
257 :
258 : #pragma pack(push, 1)
259 :
260 : #define SPDK_MD_MASK_TYPE_USED_PAGES 0
261 : #define SPDK_MD_MASK_TYPE_USED_CLUSTERS 1
262 : #define SPDK_MD_MASK_TYPE_USED_BLOBIDS 2
263 :
264 : struct spdk_bs_md_mask {
265 : uint8_t type;
266 : uint32_t length; /* In bits */
267 : uint8_t mask[0];
268 : };
269 :
270 : #define SPDK_MD_DESCRIPTOR_TYPE_PADDING 0
271 : #define SPDK_MD_DESCRIPTOR_TYPE_XATTR 2
272 : #define SPDK_MD_DESCRIPTOR_TYPE_FLAGS 3
273 : #define SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL 4
274 :
275 : /* Following descriptors define cluster layout in a blob.
276 : * EXTENT_RLE cannot be present in blobs metadata,
277 : * at the same time as EXTENT_TABLE and EXTENT_PAGE descriptors. */
278 :
279 : /* EXTENT_RLE descriptor holds an array of LBA that points to
280 : * beginning of allocated clusters. The array is run-length encoded,
281 : * with 0's being unallocated clusters. It is part of serialized
282 : * metadata chain for a blob. */
283 : #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE 1
284 : /* EXTENT_TABLE descriptor holds array of md page offsets that
285 : * point to pages with EXTENT_PAGE descriptor. The 0's in the array
286 : * are run-length encoded, non-zero values are unallocated pages.
287 : * It is part of serialized metadata chain for a blob. */
288 : #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE 5
289 : /* EXTENT_PAGE descriptor holds an array of LBAs that point to
290 : * beginning of allocated clusters. The array is run-length encoded,
291 : * with 0's being unallocated clusters. It is NOT part of
292 : * serialized metadata chain for a blob. */
293 : #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE 6
294 :
295 : struct spdk_blob_md_descriptor_xattr {
296 : uint8_t type;
297 : uint32_t length;
298 :
299 : uint16_t name_length;
300 : uint16_t value_length;
301 :
302 : char name[0];
303 : /* String name immediately followed by string value. */
304 : };
305 :
306 : struct spdk_blob_md_descriptor_extent_rle {
307 : uint8_t type;
308 : uint32_t length;
309 :
310 : struct {
311 : uint32_t cluster_idx;
312 : uint32_t length; /* In units of clusters */
313 : } extents[0];
314 : };
315 :
316 : struct spdk_blob_md_descriptor_extent_table {
317 : uint8_t type;
318 : uint32_t length;
319 :
320 : /* Number of data clusters in the blob */
321 : uint64_t num_clusters;
322 :
323 : struct {
324 : uint32_t page_idx;
325 : uint32_t num_pages; /* In units of pages */
326 : } extent_page[0];
327 : };
328 :
329 : struct spdk_blob_md_descriptor_extent_page {
330 : uint8_t type;
331 : uint32_t length;
332 :
333 : /* First cluster index in this extent page */
334 : uint32_t start_cluster_idx;
335 :
336 : uint32_t cluster_idx[0];
337 : };
338 :
339 : #define SPDK_BLOB_THIN_PROV (1ULL << 0)
340 : #define SPDK_BLOB_INTERNAL_XATTR (1ULL << 1)
341 : #define SPDK_BLOB_EXTENT_TABLE (1ULL << 2)
342 : #define SPDK_BLOB_EXTERNAL_SNAPSHOT (1ULL << 3)
343 : #define SPDK_BLOB_INVALID_FLAGS_MASK (SPDK_BLOB_THIN_PROV | SPDK_BLOB_INTERNAL_XATTR | \
344 : SPDK_BLOB_EXTENT_TABLE | SPDK_BLOB_EXTERNAL_SNAPSHOT)
345 :
346 : #define SPDK_BLOB_READ_ONLY (1ULL << 0)
347 : #define SPDK_BLOB_DATA_RO_FLAGS_MASK SPDK_BLOB_READ_ONLY
348 :
349 : #define SPDK_BLOB_CLEAR_METHOD_SHIFT 0
350 : #define SPDK_BLOB_CLEAR_METHOD (3ULL << SPDK_BLOB_CLEAR_METHOD_SHIFT)
351 : #define SPDK_BLOB_MD_RO_FLAGS_MASK SPDK_BLOB_CLEAR_METHOD
352 :
353 : struct spdk_blob_md_descriptor_flags {
354 : uint8_t type;
355 : uint32_t length;
356 :
357 : /*
358 : * If a flag in invalid_flags is set that the application is not aware of,
359 : * it will not allow the blob to be opened.
360 : */
361 : uint64_t invalid_flags;
362 :
363 : /*
364 : * If a flag in data_ro_flags is set that the application is not aware of,
365 : * allow the blob to be opened in data_read_only and md_read_only mode.
366 : */
367 : uint64_t data_ro_flags;
368 :
369 : /*
370 : * If a flag in md_ro_flags is set the application is not aware of,
371 : * allow the blob to be opened in md_read_only mode.
372 : */
373 : uint64_t md_ro_flags;
374 : };
375 :
376 : struct spdk_blob_md_descriptor {
377 : uint8_t type;
378 : uint32_t length;
379 : };
380 :
381 : #define SPDK_INVALID_MD_PAGE UINT32_MAX
382 :
383 : struct spdk_blob_md_page {
384 : spdk_blob_id id;
385 :
386 : uint32_t sequence_num;
387 : uint32_t reserved0;
388 :
389 : /* Descriptors here */
390 : uint8_t descriptors[4072];
391 :
392 : uint32_t next;
393 : uint32_t crc;
394 : };
395 : #define SPDK_BS_PAGE_SIZE 0x1000
396 : SPDK_STATIC_ASSERT(SPDK_BS_PAGE_SIZE == sizeof(struct spdk_blob_md_page), "Invalid md page size");
397 :
398 : #define SPDK_BS_MAX_DESC_SIZE SPDK_SIZEOF_MEMBER(struct spdk_blob_md_page, descriptors)
399 :
400 : /* Maximum number of extents a single Extent Page can fit.
401 : * For an SPDK_BS_PAGE_SIZE of 4K SPDK_EXTENTS_PER_EP would be 512. */
402 : #define SPDK_EXTENTS_PER_EP_MAX ((SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_extent_page)) / sizeof(uint32_t))
403 : #define SPDK_EXTENTS_PER_EP (spdk_align64pow2(SPDK_EXTENTS_PER_EP_MAX + 1) >> 1u)
404 :
405 : #define SPDK_BS_SUPER_BLOCK_SIG "SPDKBLOB"
406 :
407 : struct spdk_bs_super_block {
408 : uint8_t signature[8];
409 : uint32_t version;
410 : uint32_t length;
411 : uint32_t clean; /* If there was a clean shutdown, this is 1. */
412 : spdk_blob_id super_blob;
413 :
414 : uint32_t cluster_size; /* In bytes */
415 :
416 : uint32_t used_page_mask_start; /* Offset from beginning of disk, in pages */
417 : uint32_t used_page_mask_len; /* Count, in pages */
418 :
419 : uint32_t used_cluster_mask_start; /* Offset from beginning of disk, in pages */
420 : uint32_t used_cluster_mask_len; /* Count, in pages */
421 :
422 : uint32_t md_start; /* Offset from beginning of disk, in pages */
423 : uint32_t md_len; /* Count, in pages */
424 :
425 : struct spdk_bs_type bstype; /* blobstore type */
426 :
427 : uint32_t used_blobid_mask_start; /* Offset from beginning of disk, in pages */
428 : uint32_t used_blobid_mask_len; /* Count, in pages */
429 :
430 : uint64_t size; /* size of blobstore in bytes */
431 : uint32_t io_unit_size; /* Size of io unit in bytes */
432 :
433 : uint8_t reserved[4000];
434 : uint32_t crc;
435 : };
436 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block) == 0x1000, "Invalid super block size");
437 :
438 : #pragma pack(pop)
439 :
440 : struct spdk_bs_dev *bs_create_zeroes_dev(void);
441 : struct spdk_bs_dev *bs_create_blob_bs_dev(struct spdk_blob *blob);
442 : struct spdk_io_channel *blob_esnap_get_io_channel(struct spdk_io_channel *ch,
443 : struct spdk_blob *blob);
444 :
445 : /* Unit Conversions
446 : *
447 : * The blobstore works with several different units:
448 : * - Byte: Self explanatory
449 : * - LBA: The logical blocks on the backing storage device.
450 : * - Page: The read/write units of blobs and metadata. This is
451 : * an offset into a blob in units of 4KiB.
452 : * - Cluster Index: The disk is broken into a sequential list of
453 : * clusters. This is the offset from the beginning.
454 : *
455 : * NOTE: These conversions all act on simple magnitudes, not with any sort
456 : * of knowledge about the blobs themselves. For instance, converting
457 : * a page to an lba with the conversion function below simply converts
458 : * a number of pages to an equivalent number of lbas, but that
459 : * lba certainly isn't the right lba that corresponds to a page offset
460 : * for a particular blob.
461 : */
462 : static inline uint64_t
463 32356 : bs_byte_to_lba(struct spdk_blob_store *bs, uint64_t length)
464 : {
465 32356 : assert(length % bs->dev->blocklen == 0);
466 :
467 32356 : return length / bs->dev->blocklen;
468 : }
469 :
470 : static inline uint64_t
471 2636 : bs_dev_byte_to_lba(struct spdk_bs_dev *bs_dev, uint64_t length)
472 : {
473 2636 : assert(length % bs_dev->blocklen == 0);
474 :
475 2636 : return length / bs_dev->blocklen;
476 : }
477 :
478 : static inline uint64_t
479 26930 : bs_page_to_lba(struct spdk_blob_store *bs, uint64_t page)
480 : {
481 26930 : return page * SPDK_BS_PAGE_SIZE / bs->dev->blocklen;
482 : }
483 :
484 : static inline uint64_t
485 18422 : bs_md_page_to_lba(struct spdk_blob_store *bs, uint32_t page)
486 : {
487 18422 : assert(page < bs->md_len);
488 18422 : return bs_page_to_lba(bs, page + bs->md_start);
489 : }
490 :
491 : static inline uint64_t
492 2684 : bs_dev_page_to_lba(struct spdk_bs_dev *bs_dev, uint64_t page)
493 : {
494 2684 : return page * SPDK_BS_PAGE_SIZE / bs_dev->blocklen;
495 : }
496 :
497 : static inline uint64_t
498 161126 : bs_io_unit_per_page(struct spdk_blob_store *bs)
499 : {
500 161126 : return SPDK_BS_PAGE_SIZE / bs->io_unit_size;
501 : }
502 :
503 : static inline uint64_t
504 79766 : bs_io_unit_to_page(struct spdk_blob_store *bs, uint64_t io_unit)
505 : {
506 79766 : return io_unit / bs_io_unit_per_page(bs);
507 : }
508 :
509 : static inline uint64_t
510 28 : bs_cluster_to_page(struct spdk_blob_store *bs, uint32_t cluster)
511 : {
512 28 : return (uint64_t)cluster * bs->pages_per_cluster;
513 : }
514 :
515 : static inline uint32_t
516 408 : bs_page_to_cluster(struct spdk_blob_store *bs, uint64_t page)
517 : {
518 408 : assert(page % bs->pages_per_cluster == 0);
519 :
520 408 : return page / bs->pages_per_cluster;
521 : }
522 :
523 : static inline uint64_t
524 1136010 : bs_cluster_to_lba(struct spdk_blob_store *bs, uint32_t cluster)
525 : {
526 1136010 : assert(bs->cluster_sz / bs->dev->blocklen > 0);
527 :
528 1136010 : return (uint64_t)cluster * (bs->cluster_sz / bs->dev->blocklen);
529 : }
530 :
531 : static inline uint32_t
532 1069595 : bs_lba_to_cluster(struct spdk_blob_store *bs, uint64_t lba)
533 : {
534 1069595 : assert(lba % (bs->cluster_sz / bs->dev->blocklen) == 0);
535 :
536 1069595 : return lba / (bs->cluster_sz / bs->dev->blocklen);
537 : }
538 :
539 : static inline uint64_t
540 6464 : bs_io_unit_to_back_dev_lba(struct spdk_blob *blob, uint64_t io_unit)
541 : {
542 6464 : return io_unit * (blob->bs->io_unit_size / blob->back_bs_dev->blocklen);
543 : }
544 :
545 : static inline uint64_t
546 4712 : bs_cluster_to_extent_table_id(uint64_t cluster_num)
547 : {
548 4712 : return cluster_num / SPDK_EXTENTS_PER_EP;
549 : }
550 :
551 : static inline uint32_t *
552 4712 : bs_cluster_to_extent_page(struct spdk_blob *blob, uint64_t cluster_num)
553 : {
554 4712 : uint64_t extent_table_id = bs_cluster_to_extent_table_id(cluster_num);
555 :
556 4712 : assert(blob->use_extent_table);
557 4712 : assert(extent_table_id < blob->active.extent_pages_array_size);
558 :
559 4712 : return &blob->active.extent_pages[extent_table_id];
560 : }
561 :
562 : static inline uint64_t
563 42438 : bs_io_units_per_cluster(struct spdk_blob *blob)
564 : {
565 : uint64_t io_units_per_cluster;
566 42438 : uint8_t shift = blob->bs->pages_per_cluster_shift;
567 :
568 42438 : if (shift != 0) {
569 42438 : io_units_per_cluster = bs_io_unit_per_page(blob->bs) << shift;
570 : } else {
571 0 : io_units_per_cluster = bs_io_unit_per_page(blob->bs) * blob->bs->pages_per_cluster;
572 : }
573 :
574 42438 : return io_units_per_cluster;
575 : }
576 :
577 : /* End basic conversions */
578 :
579 : static inline uint64_t
580 30629 : bs_blobid_to_page(spdk_blob_id id)
581 : {
582 30629 : return id & 0xFFFFFFFF;
583 : }
584 :
585 : /* The blob id is a 64 bit number. The lower 32 bits are the page_idx. The upper
586 : * 32 bits are not currently used. Stick a 1 there just to catch bugs where the
587 : * code assumes blob id == page_idx.
588 : */
589 : static inline spdk_blob_id
590 2518 : bs_page_to_blobid(uint64_t page_idx)
591 : {
592 2518 : if (page_idx > UINT32_MAX) {
593 0 : return SPDK_BLOBID_INVALID;
594 : }
595 2518 : return SPDK_BLOB_BLOBID_HIGH_BIT | page_idx;
596 : }
597 :
598 : /* Given an io unit offset into a blob, look up the LBA for the
599 : * start of that io unit.
600 : */
601 : static inline uint64_t
602 37342 : bs_blob_io_unit_to_lba(struct spdk_blob *blob, uint64_t io_unit)
603 : {
604 : uint64_t lba;
605 : uint64_t pages_per_cluster;
606 : uint8_t shift;
607 : uint64_t io_units_per_cluster;
608 : uint64_t io_units_per_page;
609 : uint64_t page;
610 :
611 37342 : page = bs_io_unit_to_page(blob->bs, io_unit);
612 :
613 37342 : pages_per_cluster = blob->bs->pages_per_cluster;
614 37342 : shift = blob->bs->pages_per_cluster_shift;
615 37342 : io_units_per_page = bs_io_unit_per_page(blob->bs);
616 :
617 37342 : assert(page < blob->active.num_clusters * pages_per_cluster);
618 :
619 37342 : if (shift != 0) {
620 37342 : io_units_per_cluster = io_units_per_page << shift;
621 37342 : lba = blob->active.clusters[page >> shift];
622 : } else {
623 0 : io_units_per_cluster = io_units_per_page * pages_per_cluster;
624 0 : lba = blob->active.clusters[page / pages_per_cluster];
625 : }
626 37342 : lba += io_unit % io_units_per_cluster;
627 37342 : return lba;
628 : }
629 :
630 : /* Given an io_unit offset into a blob, look up the number of io_units until the
631 : * next cluster boundary.
632 : */
633 : static inline uint32_t
634 42298 : bs_num_io_units_to_cluster_boundary(struct spdk_blob *blob, uint64_t io_unit)
635 : {
636 : uint64_t io_units_per_cluster;
637 :
638 42298 : io_units_per_cluster = bs_io_units_per_cluster(blob);
639 :
640 42298 : return io_units_per_cluster - (io_unit % io_units_per_cluster);
641 : }
642 :
643 : /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */
644 : static inline uint32_t
645 872 : bs_io_unit_to_cluster_start(struct spdk_blob *blob, uint64_t io_unit)
646 : {
647 : uint64_t pages_per_cluster;
648 : uint64_t page;
649 :
650 872 : pages_per_cluster = blob->bs->pages_per_cluster;
651 872 : page = bs_io_unit_to_page(blob->bs, io_unit);
652 :
653 872 : return page - (page % pages_per_cluster);
654 : }
655 :
656 : /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */
657 : static inline uint32_t
658 872 : bs_io_unit_to_cluster_number(struct spdk_blob *blob, uint64_t io_unit)
659 : {
660 872 : uint64_t pages_per_cluster = blob->bs->pages_per_cluster;
661 872 : uint8_t shift = blob->bs->pages_per_cluster_shift;
662 : uint32_t page_offset;
663 :
664 872 : page_offset = io_unit / bs_io_unit_per_page(blob->bs);
665 872 : if (shift != 0) {
666 872 : return page_offset >> shift;
667 : } else {
668 0 : return page_offset / pages_per_cluster;
669 : }
670 : }
671 :
672 : /* Given an io unit offset into a blob, look up if it is from allocated cluster. */
673 : static inline bool
674 40978 : bs_io_unit_is_allocated(struct spdk_blob *blob, uint64_t io_unit)
675 : {
676 : uint64_t lba;
677 : uint64_t page;
678 : uint64_t pages_per_cluster;
679 : uint8_t shift;
680 :
681 40978 : shift = blob->bs->pages_per_cluster_shift;
682 40978 : pages_per_cluster = blob->bs->pages_per_cluster;
683 40978 : page = bs_io_unit_to_page(blob->bs, io_unit);
684 :
685 40978 : assert(page < blob->active.num_clusters * pages_per_cluster);
686 :
687 40978 : if (shift != 0) {
688 40978 : lba = blob->active.clusters[page >> shift];
689 : } else {
690 0 : lba = blob->active.clusters[page / pages_per_cluster];
691 : }
692 :
693 40978 : if (lba == 0) {
694 3356 : assert(spdk_blob_is_thin_provisioned(blob));
695 3356 : return false;
696 : } else {
697 37622 : return true;
698 : }
699 : }
700 :
701 : #endif
|