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