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_open_zones(const struct spdk_bdev *bdev) 49 { 50 return bdev->max_open_zones; 51 } 52 53 uint32_t 54 spdk_bdev_get_max_active_zones(const struct spdk_bdev *bdev) 55 { 56 return bdev->max_active_zones; 57 } 58 59 uint32_t 60 spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev) 61 { 62 return bdev->optimal_open_zones; 63 } 64 65 int 66 spdk_bdev_get_zone_info(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 67 uint64_t zone_id, size_t num_zones, struct spdk_bdev_zone_info *info, 68 spdk_bdev_io_completion_cb cb, void *cb_arg) 69 { 70 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 71 struct spdk_bdev_io *bdev_io; 72 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 73 74 bdev_io = bdev_channel_get_io(channel); 75 if (!bdev_io) { 76 return -ENOMEM; 77 } 78 79 bdev_io->internal.ch = channel; 80 bdev_io->internal.desc = desc; 81 bdev_io->type = SPDK_BDEV_IO_TYPE_GET_ZONE_INFO; 82 bdev_io->u.zone_mgmt.zone_id = zone_id; 83 bdev_io->u.zone_mgmt.num_zones = num_zones; 84 bdev_io->u.zone_mgmt.buf = info; 85 bdev_io_init(bdev_io, bdev, cb_arg, cb); 86 87 bdev_io_submit(bdev_io); 88 return 0; 89 } 90 91 int 92 spdk_bdev_zone_management(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 93 uint64_t zone_id, enum spdk_bdev_zone_action action, 94 spdk_bdev_io_completion_cb cb, void *cb_arg) 95 { 96 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 97 struct spdk_bdev_io *bdev_io; 98 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 99 100 bdev_io = bdev_channel_get_io(channel); 101 if (!bdev_io) { 102 return -ENOMEM; 103 } 104 105 bdev_io->internal.ch = channel; 106 bdev_io->internal.desc = desc; 107 bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT; 108 bdev_io->u.zone_mgmt.zone_action = action; 109 bdev_io->u.zone_mgmt.zone_id = zone_id; 110 bdev_io->u.zone_mgmt.num_zones = 1; 111 bdev_io_init(bdev_io, bdev, cb_arg, cb); 112 113 bdev_io_submit(bdev_io); 114 return 0; 115 } 116 117 static int 118 zone_bdev_append_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 119 void *buf, void *md_buf, uint64_t zone_id, uint64_t num_blocks, 120 spdk_bdev_io_completion_cb cb, void *cb_arg) 121 { 122 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 123 struct spdk_bdev_io *bdev_io; 124 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 125 126 bdev_io = bdev_channel_get_io(channel); 127 if (!bdev_io) { 128 return -ENOMEM; 129 } 130 131 bdev_io->internal.ch = channel; 132 bdev_io->internal.desc = desc; 133 bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_APPEND; 134 bdev_io->u.bdev.iovs = &bdev_io->iov; 135 bdev_io->u.bdev.iovs[0].iov_base = buf; 136 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 137 bdev_io->u.bdev.iovcnt = 1; 138 bdev_io->u.bdev.md_buf = md_buf; 139 bdev_io->u.bdev.num_blocks = num_blocks; 140 bdev_io->u.bdev.offset_blocks = zone_id; 141 bdev_io_init(bdev_io, bdev, cb_arg, cb); 142 143 bdev_io_submit(bdev_io); 144 return 0; 145 } 146 147 int 148 spdk_bdev_zone_append(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 149 void *buf, uint64_t start_lba, uint64_t num_blocks, 150 spdk_bdev_io_completion_cb cb, void *cb_arg) 151 { 152 return zone_bdev_append_with_md(desc, ch, buf, NULL, start_lba, num_blocks, 153 cb, cb_arg); 154 } 155 156 int 157 spdk_bdev_zone_append_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 158 void *buf, void *md, uint64_t start_lba, uint64_t num_blocks, 159 spdk_bdev_io_completion_cb cb, void *cb_arg) 160 { 161 return zone_bdev_append_with_md(desc, ch, buf, md, start_lba, num_blocks, 162 cb, cb_arg); 163 } 164 165 int 166 spdk_bdev_zone_appendv_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 167 struct iovec *iov, int iovcnt, void *md_buf, uint64_t zone_id, 168 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, 169 void *cb_arg) 170 { 171 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 172 struct spdk_bdev_io *bdev_io; 173 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 174 175 bdev_io = bdev_channel_get_io(channel); 176 if (!bdev_io) { 177 return -ENOMEM; 178 } 179 180 bdev_io->internal.ch = channel; 181 bdev_io->internal.desc = desc; 182 bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_APPEND; 183 bdev_io->u.bdev.iovs = iov; 184 bdev_io->u.bdev.iovcnt = iovcnt; 185 bdev_io->u.bdev.md_buf = md_buf; 186 bdev_io->u.bdev.num_blocks = num_blocks; 187 bdev_io->u.bdev.offset_blocks = zone_id; 188 bdev_io_init(bdev_io, bdev, cb_arg, cb); 189 190 bdev_io_submit(bdev_io); 191 return 0; 192 } 193 194 int 195 spdk_bdev_zone_appendv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 196 struct iovec *iovs, int iovcnt, uint64_t zone_id, uint64_t num_blocks, 197 spdk_bdev_io_completion_cb cb, void *cb_arg) 198 { 199 return spdk_bdev_zone_appendv_with_md(desc, ch, iovs, iovcnt, NULL, zone_id, num_blocks, 200 cb, cb_arg); 201 } 202 203 uint64_t 204 spdk_bdev_io_get_append_location(struct spdk_bdev_io *bdev_io) 205 { 206 return bdev_io->u.bdev.offset_blocks; 207 } 208