1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/* $OpenBSD: softraid_raid0.c,v 1.2 2008/01/24 13:58:14 marco Exp $ */
/*
* Copyright (c) 2008 Marco Peereboom <marco@peereboom.us>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "bio.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/disk.h>
#include <sys/rwlock.h>
#include <sys/queue.h>
#include <sys/fcntl.h>
#include <sys/disklabel.h>
#include <sys/mount.h>
#include <sys/sensors.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <scsi/scsi_disk.h>
#include <dev/softraidvar.h>
#include <dev/rndvar.h>
/* RAID 0 functions */
int
sr_raid0_alloc_resources(struct sr_discipline *sd)
{
int rv = EINVAL;
if (!sd)
return (rv);
DNPRINTF(SR_D_DIS, "%s: sr_raid0_alloc_resources\n",
DEVNAME(sd->sd_sc));
if (sr_alloc_wu(sd))
goto bad;
if (sr_alloc_ccb(sd))
goto bad;
/* setup runtime values */
sd->mds.mdd_raid0.sr0_strip_bits =
sr_validate_stripsize(sd->sd_vol.sv_meta.svm_strip_size);
if (sd->mds.mdd_raid0.sr0_strip_bits == -1)
goto bad;
rv = 0;
bad:
return (rv);
}
int
sr_raid0_free_resources(struct sr_discipline *sd)
{
int rv = EINVAL;
if (!sd)
return (rv);
DNPRINTF(SR_D_DIS, "%s: sr_raid0_free_resources\n",
DEVNAME(sd->sd_sc));
sr_free_wu(sd);
sr_free_ccb(sd);
if (sd->sd_meta)
free(sd->sd_meta, M_DEVBUF);
rv = 0;
return (rv);
}
int
sr_raid0_rw(struct sr_workunit *wu)
{
struct sr_discipline *sd = wu->swu_dis;
struct scsi_xfer *xs = wu->swu_xs;
struct sr_workunit *wup;
struct sr_ccb *ccb;
struct sr_chunk *scp;
int s;
daddr64_t blk, lbaoffs, strip_no, chunk, stripoffs, strip_bits;
daddr64_t strip_size, no_chunk, chunkoffs, physoffs, length, leftover;
u_int8_t *data;
DNPRINTF(SR_D_DIS, "%s: sr_raid0_rw 0x%02x\n", DEVNAME(sd->sd_sc),
xs->cmd->opcode);
if (sd->sd_vol.sv_meta.svm_status == BIOC_SVOFFLINE) {
DNPRINTF(SR_D_DIS, "%s: sr_raid0_rw device offline\n",
DEVNAME(sd->sd_sc));
goto bad;
}
if (xs->datalen == 0) {
printf("%s: %s: illegal block count\n",
DEVNAME(sd->sd_sc), sd->sd_vol.sv_meta.svm_devname);
goto bad;
}
if (xs->cmdlen == 10)
blk = _4btol(((struct scsi_rw_big *)xs->cmd)->addr);
else if (xs->cmdlen == 6)
blk = _3btol(((struct scsi_rw *)xs->cmd)->addr);
else {
printf("%s: %s: illegal cmdlen\n", DEVNAME(sd->sd_sc),
sd->sd_vol.sv_meta.svm_devname);
goto bad;
}
wu->swu_blk_start = blk;
wu->swu_blk_end = blk + (xs->datalen >> DEV_BSHIFT) - 1;
if (wu->swu_blk_end > sd->sd_vol.sv_meta.svm_size) {
DNPRINTF(SR_D_DIS, "%s: sr_raid0_rw out of bounds start: %lld "
"end: %lld length: %d\n", wu->swu_blk_start,
wu->swu_blk_end, xs->datalen);
sd->sd_scsi_sense.error_code = SSD_ERRCODE_CURRENT |
SSD_ERRCODE_VALID;
sd->sd_scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
sd->sd_scsi_sense.add_sense_code = 0x21;
sd->sd_scsi_sense.add_sense_code_qual = 0x00;
sd->sd_scsi_sense.extra_len = 4;
goto bad;
}
strip_size = sd->sd_vol.sv_meta.svm_strip_size;
strip_bits = sd->mds.mdd_raid0.sr0_strip_bits;
no_chunk = sd->sd_vol.sv_meta.svm_no_chunk;
DNPRINTF(SR_D_DIS, "%s: %s: front end io: lba %lld size %d\n",
DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname, blk, xs->datalen);
/* all offs are in bytes */
lbaoffs = blk << DEV_BSHIFT;
strip_no = lbaoffs >> strip_bits;
chunk = strip_no % no_chunk;
stripoffs = lbaoffs & (strip_size - 1);
chunkoffs = (strip_no / no_chunk) << strip_bits;
physoffs = chunkoffs + stripoffs +
((SR_META_OFFSET + SR_META_SIZE) << DEV_BSHIFT);
length = MIN(xs->datalen, strip_size - stripoffs);
leftover = xs->datalen;
data = xs->data;
for (wu->swu_io_count = 1;; wu->swu_io_count++) {
/* make sure chunk is online */
scp = sd->sd_vol.sv_chunks[chunk];
if (scp->src_meta.scm_status != BIOC_SDONLINE) {
sr_put_ccb(ccb);
goto bad;
}
ccb = sr_get_ccb(sd);
if (!ccb) {
/* should never happen but handle more gracefully */
printf("%s: %s: too many ccbs queued\n",
DEVNAME(sd->sd_sc),
sd->sd_vol.sv_meta.svm_devname);
goto bad;
}
DNPRINTF(SR_D_DIS, "%s: %s raid io: lbaoffs: %lld "
"strip_no: %lld chunk: %lld stripoffs: %lld "
"chunkoffs: %lld physoffs: %lld length: %lld "
"leftover: %lld data: %p\n",
DEVNAME(sc), sd->sd_vol.sv_meta.svm_devname, lbaoffs,
strip_no, chunk, stripoffs, chunkoffs, physoffs, length,
leftover, data);
ccb->ccb_buf.b_flags = B_CALL;
ccb->ccb_buf.b_iodone = sr_raid0_intr;
ccb->ccb_buf.b_blkno = physoffs >> DEV_BSHIFT;
ccb->ccb_buf.b_bcount = length;
ccb->ccb_buf.b_bufsize = length;
ccb->ccb_buf.b_resid = length;
ccb->ccb_buf.b_data = data;
ccb->ccb_buf.b_error = 0;
ccb->ccb_buf.b_proc = curproc;
ccb->ccb_wu = wu;
ccb->ccb_buf.b_flags |= xs->flags & SCSI_DATA_IN ?
B_READ : B_WRITE;
ccb->ccb_target = chunk;
ccb->ccb_buf.b_dev = sd->sd_vol.sv_chunks[chunk]->src_dev_mm;
ccb->ccb_buf.b_vp = NULL;
LIST_INIT(&ccb->ccb_buf.b_dep);
TAILQ_INSERT_TAIL(&wu->swu_ccb, ccb, ccb_link);
DNPRINTF(SR_D_DIS, "%s: %s: sr_raid0: b_bcount: %d "
"b_blkno: %lld b_flags 0x%0x b_data %p\n",
DEVNAME(sd->sd_sc), sd->sd_vol.sv_meta.svm_devname,
ccb->ccb_buf.b_bcount, ccb->ccb_buf.b_blkno,
ccb->ccb_buf.b_flags, ccb->ccb_buf.b_data);
leftover -= length;
if (leftover == 0)
break;
data += length;
if (++chunk > no_chunk - 1) {
chunk = 0;
physoffs += length;
} else if (wu->swu_io_count == 1)
physoffs -= stripoffs;
length = MIN(leftover,strip_size);
}
s = splbio();
/* walk queue backwards and fill in collider if we have one */
TAILQ_FOREACH_REVERSE(wup, &sd->sd_wu_pendq, sr_wu_list, swu_link) {
if (wu->swu_blk_end < wup->swu_blk_start ||
wup->swu_blk_end < wu->swu_blk_start)
continue;
/* we have an LBA collision, defer wu */
wu->swu_state = SR_WU_DEFERRED;
if (wup->swu_collider)
/* wu is on deferred queue, append to last wu */
while (wup->swu_collider)
wup = wup->swu_collider;
wup->swu_collider = wu;
TAILQ_INSERT_TAIL(&sd->sd_wu_defq, wu, swu_link);
sd->sd_wu_collisions++;
goto queued;
}
sr_raid_startwu(wu);
queued:
splx(s);
return (0);
bad:
/* wu is unwound by sr_put_wu */
return (1);
}
void
sr_raid0_intr(struct buf *bp)
{
struct sr_ccb *ccb = (struct sr_ccb *)bp;
struct sr_workunit *wu = ccb->ccb_wu, *wup;
struct sr_discipline *sd = wu->swu_dis;
struct scsi_xfer *xs = wu->swu_xs;
struct sr_softc *sc = sd->sd_sc;
int s, pend;
DNPRINTF(SR_D_INTR, "%s: sr_intr bp %x xs %x\n",
DEVNAME(sc), bp, xs);
DNPRINTF(SR_D_INTR, "%s: sr_intr: b_bcount: %d b_resid: %d"
" b_flags: 0x%0x block: %lld target: %d\n", DEVNAME(sc),
ccb->ccb_buf.b_bcount, ccb->ccb_buf.b_resid, ccb->ccb_buf.b_flags,
ccb->ccb_buf.b_blkno, ccb->ccb_target);
s = splbio();
if (ccb->ccb_buf.b_flags & B_ERROR) {
printf("%s: i/o error on block %lld target: %d b_error: %d\n",
DEVNAME(sc), ccb->ccb_buf.b_blkno, ccb->ccb_target,
ccb->ccb_buf.b_error);
DNPRINTF(SR_D_INTR, "%s: i/o error on block %lld target: %d\n",
DEVNAME(sc), ccb->ccb_buf.b_blkno, ccb->ccb_target);
wu->swu_ios_failed++;
ccb->ccb_state = SR_CCB_FAILED;
if (ccb->ccb_target != -1)
sd->sd_set_chunk_state(sd, ccb->ccb_target,
BIOC_SDOFFLINE);
else
panic("%s: invalid target on wu: %p", DEVNAME(sc), wu);
} else {
ccb->ccb_state = SR_CCB_OK;
wu->swu_ios_succeeded++;
}
wu->swu_ios_complete++;
DNPRINTF(SR_D_INTR, "%s: sr_intr: comp: %d count: %d failed: %d\n",
DEVNAME(sc), wu->swu_ios_complete, wu->swu_io_count,
wu->swu_ios_failed);
if (wu->swu_ios_complete >= wu->swu_io_count) {
if (wu->swu_ios_failed)
goto bad;
xs->error = XS_NOERROR;
xs->resid = 0;
xs->flags |= ITSDONE;
pend = 0;
TAILQ_FOREACH(wup, &sd->sd_wu_pendq, swu_link) {
if (wu == wup) {
/* wu on pendq, remove */
TAILQ_REMOVE(&sd->sd_wu_pendq, wu, swu_link);
pend = 1;
if (wu->swu_collider) {
/* restart deferred wu */
wu->swu_collider->swu_state =
SR_WU_INPROGRESS;
TAILQ_REMOVE(&sd->sd_wu_defq,
wu->swu_collider, swu_link);
sr_raid_startwu(wu->swu_collider);
}
break;
}
}
if (!pend)
printf("%s: wu: %p not on pending queue\n",
DEVNAME(sc), wu);
/* do not change the order of these 2 functions */
sr_put_wu(wu);
scsi_done(xs);
if (sd->sd_sync && sd->sd_wu_pending == 0)
wakeup(sd);
}
splx(s);
return;
bad:
xs->error = XS_DRIVER_STUFFUP;
xs->flags |= ITSDONE;
sr_put_wu(wu);
scsi_done(xs);
splx(s);
}
|