1 /* $OpenBSD: softraid_concat.c,v 1.21 2013/11/22 03:47:07 krw Exp $ */ 2 /* 3 * Copyright (c) 2008 Marco Peereboom <marco@peereboom.us> 4 * Copyright (c) 2011 Joel Sing <jsing@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include "bio.h" 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/device.h> 24 #include <sys/buf.h> 25 #include <sys/queue.h> 26 #include <sys/sensors.h> 27 28 #include <scsi/scsi_all.h> 29 #include <scsi/scsiconf.h> 30 #include <scsi/scsi_disk.h> 31 32 #include <dev/softraidvar.h> 33 34 /* CONCAT functions. */ 35 int sr_concat_create(struct sr_discipline *, struct bioc_createraid *, 36 int, int64_t); 37 int sr_concat_assemble(struct sr_discipline *, struct bioc_createraid *, 38 int, void *); 39 int sr_concat_init(struct sr_discipline *); 40 int sr_concat_rw(struct sr_workunit *); 41 42 /* Discipline initialisation. */ 43 void 44 sr_concat_discipline_init(struct sr_discipline *sd) 45 { 46 /* Fill out discipline members. */ 47 sd->sd_type = SR_MD_CONCAT; 48 strlcpy(sd->sd_name, "CONCAT", sizeof(sd->sd_name)); 49 sd->sd_capabilities = SR_CAP_SYSTEM_DISK | SR_CAP_AUTO_ASSEMBLE | 50 SR_CAP_NON_COERCED; 51 sd->sd_max_wu = SR_CONCAT_NOWU; 52 53 /* Setup discipline specific function pointers. */ 54 sd->sd_assemble = sr_concat_assemble; 55 sd->sd_create = sr_concat_create; 56 sd->sd_scsi_rw = sr_concat_rw; 57 } 58 59 int 60 sr_concat_create(struct sr_discipline *sd, struct bioc_createraid *bc, 61 int no_chunk, int64_t coerced_size) 62 { 63 int i; 64 65 if (no_chunk < 2) { 66 sr_error(sd->sd_sc, "%s requires two or more chunks", 67 sd->sd_name); 68 return EINVAL; 69 } 70 71 sd->sd_meta->ssdi.ssd_size = 0; 72 for (i = 0; i < no_chunk; i++) 73 sd->sd_meta->ssdi.ssd_size += 74 sd->sd_vol.sv_chunks[i]->src_size; 75 76 return sr_concat_init(sd); 77 } 78 79 int 80 sr_concat_assemble(struct sr_discipline *sd, struct bioc_createraid *bc, 81 int no_chunk, void *data) 82 { 83 return sr_concat_init(sd); 84 } 85 86 int 87 sr_concat_init(struct sr_discipline *sd) 88 { 89 sd->sd_max_ccb_per_wu = SR_CONCAT_NOWU * sd->sd_meta->ssdi.ssd_chunk_no; 90 91 return 0; 92 } 93 94 int 95 sr_concat_rw(struct sr_workunit *wu) 96 { 97 struct sr_discipline *sd = wu->swu_dis; 98 struct scsi_xfer *xs = wu->swu_xs; 99 struct sr_ccb *ccb; 100 struct sr_chunk *scp; 101 daddr_t blk; 102 int64_t lbaoffs, physoffs; 103 int64_t no_chunk, chunkend, chunk, chunksize; 104 int64_t length, leftover; 105 u_int8_t *data; 106 107 /* blk and scsi error will be handled by sr_validate_io */ 108 if (sr_validate_io(wu, &blk, "sr_concat_rw")) 109 goto bad; 110 111 no_chunk = sd->sd_meta->ssdi.ssd_chunk_no; 112 113 DNPRINTF(SR_D_DIS, "%s: %s: front end io: lba %lld size %d\n", 114 DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, 115 (long long)blk, xs->datalen); 116 117 /* All offsets are in bytes. */ 118 lbaoffs = blk << DEV_BSHIFT; 119 leftover = xs->datalen; 120 data = xs->data; 121 for (;;) { 122 123 chunkend = 0; 124 physoffs = lbaoffs; 125 for (chunk = 0; chunk < no_chunk; chunk++) { 126 chunksize = sd->sd_vol.sv_chunks[chunk]->src_size << 127 DEV_BSHIFT; 128 chunkend += chunksize; 129 if (lbaoffs < chunkend) 130 break; 131 physoffs -= chunksize; 132 } 133 if (lbaoffs > chunkend) 134 goto bad; 135 136 length = MIN(MIN(leftover, chunkend - lbaoffs), MAXPHYS); 137 physoffs += sd->sd_meta->ssd_data_offset << DEV_BSHIFT; 138 139 /* make sure chunk is online */ 140 scp = sd->sd_vol.sv_chunks[chunk]; 141 if (scp->src_meta.scm_status != BIOC_SDONLINE) 142 goto bad; 143 144 DNPRINTF(SR_D_DIS, "%s: %s %s io lbaoffs %lld " 145 "chunk %lld chunkend %lld physoffs %lld length %lld " 146 "leftover %lld data %p\n", 147 DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, sd->sd_name, 148 lbaoffs, chunk, chunkend, physoffs, length, leftover, data); 149 150 blk = physoffs >> DEV_BSHIFT; 151 ccb = sr_ccb_rw(sd, chunk, blk, length, data, xs->flags, 0); 152 if (!ccb) { 153 /* should never happen but handle more gracefully */ 154 printf("%s: %s: too many ccbs queued\n", 155 DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname); 156 goto bad; 157 } 158 sr_wu_enqueue_ccb(wu, ccb); 159 160 leftover -= length; 161 if (leftover == 0) 162 break; 163 data += length; 164 lbaoffs += length; 165 } 166 167 sr_schedule_wu(wu); 168 169 return (0); 170 171 bad: 172 /* wu is unwound by sr_wu_put */ 173 return (1); 174 } 175