xref: /spdk/lib/blob/zeroes.c (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 #include "spdk/blob.h"
9 #include "spdk/dma.h"
10 
11 #include "blobstore.h"
12 
13 static void
14 zeroes_destroy(struct spdk_bs_dev *bs_dev)
15 {
16 	return;
17 }
18 
19 static void
20 zeroes_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
21 	    uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args)
22 {
23 	memset(payload, 0, dev->blocklen * lba_count);
24 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
25 }
26 
27 static void
28 zeroes_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
29 	     uint64_t lba, uint32_t lba_count,
30 	     struct spdk_bs_dev_cb_args *cb_args)
31 {
32 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
33 	assert(false);
34 }
35 
36 static void
37 zeroes_readv(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
38 	     struct iovec *iov, int iovcnt,
39 	     uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args)
40 {
41 	int i;
42 
43 	for (i = 0; i < iovcnt; i++) {
44 		memset(iov[i].iov_base, 0, iov[i].iov_len);
45 	}
46 
47 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
48 }
49 
50 static void
51 zeroes_writev(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
52 	      struct iovec *iov, int iovcnt,
53 	      uint64_t lba, uint32_t lba_count,
54 	      struct spdk_bs_dev_cb_args *cb_args)
55 {
56 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
57 	assert(false);
58 }
59 
60 static void
61 _read_memory_domain_memzero_done(void *ctx, int rc)
62 {
63 	struct spdk_bs_dev_cb_args *cb_args = (struct spdk_bs_dev_cb_args *)ctx;
64 
65 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, rc);
66 }
67 
68 static void
69 zeroes_readv_ext(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
70 		 struct iovec *iov, int iovcnt,
71 		 uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args,
72 		 struct spdk_blob_ext_io_opts *ext_io_opts)
73 {
74 	int i, rc;
75 
76 	if (ext_io_opts->memory_domain) {
77 		rc = spdk_memory_domain_memzero(ext_io_opts->memory_domain, ext_io_opts->memory_domain_ctx, iov,
78 						iovcnt, _read_memory_domain_memzero_done, cb_args);
79 		if (rc) {
80 			cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, rc);
81 		}
82 		return;
83 	}
84 
85 	for (i = 0; i < iovcnt; i++) {
86 		memset(iov[i].iov_base, 0, iov[i].iov_len);
87 	}
88 
89 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
90 }
91 
92 static void
93 zeroes_writev_ext(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
94 		  struct iovec *iov, int iovcnt,
95 		  uint64_t lba, uint32_t lba_count,
96 		  struct spdk_bs_dev_cb_args *cb_args,
97 		  struct spdk_blob_ext_io_opts *ext_io_opts)
98 {
99 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
100 	assert(false);
101 }
102 
103 static void
104 zeroes_write_zeroes(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
105 		    uint64_t lba, uint64_t lba_count,
106 		    struct spdk_bs_dev_cb_args *cb_args)
107 {
108 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
109 	assert(false);
110 }
111 
112 static void
113 zeroes_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
114 	     uint64_t lba, uint64_t lba_count,
115 	     struct spdk_bs_dev_cb_args *cb_args)
116 {
117 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
118 	assert(false);
119 }
120 
121 static bool
122 zeroes_is_zeroes(struct spdk_bs_dev *dev, uint64_t lba, uint64_t lba_count)
123 {
124 	return true;
125 }
126 
127 static struct spdk_bs_dev g_zeroes_bs_dev = {
128 	.blockcnt = UINT64_MAX,
129 	.blocklen = 512,
130 	.create_channel = NULL,
131 	.destroy_channel = NULL,
132 	.destroy = zeroes_destroy,
133 	.read = zeroes_read,
134 	.write = zeroes_write,
135 	.readv = zeroes_readv,
136 	.writev = zeroes_writev,
137 	.readv_ext = zeroes_readv_ext,
138 	.writev_ext = zeroes_writev_ext,
139 	.write_zeroes = zeroes_write_zeroes,
140 	.unmap = zeroes_unmap,
141 	.is_zeroes = zeroes_is_zeroes,
142 };
143 
144 struct spdk_bs_dev *
145 bs_create_zeroes_dev(void)
146 {
147 	return &g_zeroes_bs_dev;
148 }
149