1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/bdev_zone.h" 37 #include "spdk/bdev_module.h" 38 39 #include "bdev_internal.h" 40 41 uint64_t 42 spdk_bdev_get_zone_size(const struct spdk_bdev *bdev) 43 { 44 return bdev->zone_size; 45 } 46 47 uint32_t 48 spdk_bdev_get_max_zone_append_size(const struct spdk_bdev *bdev) 49 { 50 return bdev->max_zone_append_size; 51 } 52 53 uint32_t 54 spdk_bdev_get_max_open_zones(const struct spdk_bdev *bdev) 55 { 56 return bdev->max_open_zones; 57 } 58 59 uint32_t 60 spdk_bdev_get_max_active_zones(const struct spdk_bdev *bdev) 61 { 62 return bdev->max_active_zones; 63 } 64 65 uint32_t 66 spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev) 67 { 68 return bdev->optimal_open_zones; 69 } 70 71 int 72 spdk_bdev_get_zone_info(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 73 uint64_t zone_id, size_t num_zones, struct spdk_bdev_zone_info *info, 74 spdk_bdev_io_completion_cb cb, void *cb_arg) 75 { 76 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 77 struct spdk_bdev_io *bdev_io; 78 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 79 80 bdev_io = bdev_channel_get_io(channel); 81 if (!bdev_io) { 82 return -ENOMEM; 83 } 84 85 bdev_io->internal.ch = channel; 86 bdev_io->internal.desc = desc; 87 bdev_io->type = SPDK_BDEV_IO_TYPE_GET_ZONE_INFO; 88 bdev_io->u.zone_mgmt.zone_id = zone_id; 89 bdev_io->u.zone_mgmt.num_zones = num_zones; 90 bdev_io->u.zone_mgmt.buf = info; 91 bdev_io_init(bdev_io, bdev, cb_arg, cb); 92 93 bdev_io_submit(bdev_io); 94 return 0; 95 } 96 97 int 98 spdk_bdev_zone_management(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 99 uint64_t zone_id, enum spdk_bdev_zone_action action, 100 spdk_bdev_io_completion_cb cb, void *cb_arg) 101 { 102 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 103 struct spdk_bdev_io *bdev_io; 104 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 105 106 bdev_io = bdev_channel_get_io(channel); 107 if (!bdev_io) { 108 return -ENOMEM; 109 } 110 111 bdev_io->internal.ch = channel; 112 bdev_io->internal.desc = desc; 113 bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT; 114 bdev_io->u.zone_mgmt.zone_action = action; 115 bdev_io->u.zone_mgmt.zone_id = zone_id; 116 bdev_io->u.zone_mgmt.num_zones = 1; 117 bdev_io_init(bdev_io, bdev, cb_arg, cb); 118 119 bdev_io_submit(bdev_io); 120 return 0; 121 } 122 123 static int 124 zone_bdev_append_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 125 void *buf, void *md_buf, uint64_t zone_id, uint64_t num_blocks, 126 spdk_bdev_io_completion_cb cb, void *cb_arg) 127 { 128 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 129 struct spdk_bdev_io *bdev_io; 130 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 131 132 bdev_io = bdev_channel_get_io(channel); 133 if (!bdev_io) { 134 return -ENOMEM; 135 } 136 137 bdev_io->internal.ch = channel; 138 bdev_io->internal.desc = desc; 139 bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_APPEND; 140 bdev_io->u.bdev.iovs = &bdev_io->iov; 141 bdev_io->u.bdev.iovs[0].iov_base = buf; 142 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 143 bdev_io->u.bdev.iovcnt = 1; 144 bdev_io->u.bdev.md_buf = md_buf; 145 bdev_io->u.bdev.num_blocks = num_blocks; 146 bdev_io->u.bdev.offset_blocks = zone_id; 147 bdev_io_init(bdev_io, bdev, cb_arg, cb); 148 149 bdev_io_submit(bdev_io); 150 return 0; 151 } 152 153 int 154 spdk_bdev_zone_append(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 155 void *buf, uint64_t start_lba, uint64_t num_blocks, 156 spdk_bdev_io_completion_cb cb, void *cb_arg) 157 { 158 return zone_bdev_append_with_md(desc, ch, buf, NULL, start_lba, num_blocks, 159 cb, cb_arg); 160 } 161 162 int 163 spdk_bdev_zone_append_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 164 void *buf, void *md, uint64_t start_lba, uint64_t num_blocks, 165 spdk_bdev_io_completion_cb cb, void *cb_arg) 166 { 167 return zone_bdev_append_with_md(desc, ch, buf, md, start_lba, num_blocks, 168 cb, cb_arg); 169 } 170 171 int 172 spdk_bdev_zone_appendv_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 173 struct iovec *iov, int iovcnt, void *md_buf, uint64_t zone_id, 174 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, 175 void *cb_arg) 176 { 177 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 178 struct spdk_bdev_io *bdev_io; 179 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 180 181 bdev_io = bdev_channel_get_io(channel); 182 if (!bdev_io) { 183 return -ENOMEM; 184 } 185 186 bdev_io->internal.ch = channel; 187 bdev_io->internal.desc = desc; 188 bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_APPEND; 189 bdev_io->u.bdev.iovs = iov; 190 bdev_io->u.bdev.iovcnt = iovcnt; 191 bdev_io->u.bdev.md_buf = md_buf; 192 bdev_io->u.bdev.num_blocks = num_blocks; 193 bdev_io->u.bdev.offset_blocks = zone_id; 194 bdev_io_init(bdev_io, bdev, cb_arg, cb); 195 196 bdev_io_submit(bdev_io); 197 return 0; 198 } 199 200 int 201 spdk_bdev_zone_appendv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 202 struct iovec *iovs, int iovcnt, uint64_t zone_id, uint64_t num_blocks, 203 spdk_bdev_io_completion_cb cb, void *cb_arg) 204 { 205 return spdk_bdev_zone_appendv_with_md(desc, ch, iovs, iovcnt, NULL, zone_id, num_blocks, 206 cb, cb_arg); 207 } 208 209 uint64_t 210 spdk_bdev_io_get_append_location(struct spdk_bdev_io *bdev_io) 211 { 212 return bdev_io->u.bdev.offset_blocks; 213 } 214