Line data Source code
1 : /* $OpenBSD: softraid_concat.c,v 1.26 2017/04/28 23:33: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 0 : sr_concat_discipline_init(struct sr_discipline *sd)
45 : {
46 : /* Fill out discipline members. */
47 0 : sd->sd_type = SR_MD_CONCAT;
48 0 : strlcpy(sd->sd_name, "CONCAT", sizeof(sd->sd_name));
49 0 : sd->sd_capabilities = SR_CAP_SYSTEM_DISK | SR_CAP_AUTO_ASSEMBLE |
50 : SR_CAP_NON_COERCED;
51 0 : sd->sd_max_wu = SR_CONCAT_NOWU;
52 :
53 : /* Setup discipline specific function pointers. */
54 0 : sd->sd_assemble = sr_concat_assemble;
55 0 : sd->sd_create = sr_concat_create;
56 0 : sd->sd_scsi_rw = sr_concat_rw;
57 0 : }
58 :
59 : int
60 0 : 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 0 : if (no_chunk < 2) {
66 0 : sr_error(sd->sd_sc, "%s requires two or more chunks",
67 0 : sd->sd_name);
68 0 : return EINVAL;
69 : }
70 :
71 0 : sd->sd_meta->ssdi.ssd_size = 0;
72 0 : for (i = 0; i < no_chunk; i++) {
73 0 : sd->sd_meta->ssdi.ssd_size +=
74 0 : sd->sd_vol.sv_chunks[i]->src_size;
75 : }
76 :
77 0 : return sr_concat_init(sd);
78 0 : }
79 :
80 : int
81 0 : sr_concat_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
82 : int no_chunk, void *data)
83 : {
84 0 : return sr_concat_init(sd);
85 : }
86 :
87 : int
88 0 : sr_concat_init(struct sr_discipline *sd)
89 : {
90 0 : sd->sd_max_ccb_per_wu = SR_CONCAT_NOWU * sd->sd_meta->ssdi.ssd_chunk_no;
91 :
92 0 : return 0;
93 : }
94 :
95 : int
96 0 : sr_concat_rw(struct sr_workunit *wu)
97 : {
98 0 : struct sr_discipline *sd = wu->swu_dis;
99 0 : struct scsi_xfer *xs = wu->swu_xs;
100 : struct sr_ccb *ccb;
101 : struct sr_chunk *scp;
102 0 : daddr_t blkno;
103 : int64_t lbaoffs, offset;
104 : int64_t no_chunk, chunkend, chunk, chunksize;
105 : int64_t length, leftover;
106 : u_int8_t *data;
107 :
108 : /* blkno and scsi error will be handled by sr_validate_io */
109 0 : if (sr_validate_io(wu, &blkno, "sr_concat_rw"))
110 : goto bad;
111 :
112 0 : no_chunk = sd->sd_meta->ssdi.ssd_chunk_no;
113 :
114 : DNPRINTF(SR_D_DIS, "%s: %s: front end io: blkno %lld size %d\n",
115 : DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname,
116 : (long long)blkno, xs->datalen);
117 :
118 : /* All offsets are in bytes. */
119 0 : lbaoffs = blkno << DEV_BSHIFT;
120 0 : leftover = xs->datalen;
121 0 : data = xs->data;
122 0 : for (;;) {
123 :
124 : chunkend = 0;
125 : offset = lbaoffs;
126 0 : for (chunk = 0; chunk < no_chunk; chunk++) {
127 0 : chunksize = sd->sd_vol.sv_chunks[chunk]->src_size <<
128 : DEV_BSHIFT;
129 0 : chunkend += chunksize;
130 0 : if (lbaoffs < chunkend)
131 : break;
132 0 : offset -= chunksize;
133 : }
134 0 : if (lbaoffs > chunkend)
135 : goto bad;
136 :
137 0 : length = MIN(MIN(leftover, chunkend - lbaoffs), MAXPHYS);
138 :
139 : /* make sure chunk is online */
140 0 : scp = sd->sd_vol.sv_chunks[chunk];
141 0 : 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 offset %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, offset, length, leftover, data);
149 :
150 0 : blkno = offset >> DEV_BSHIFT;
151 0 : ccb = sr_ccb_rw(sd, chunk, blkno, length, data, xs->flags, 0);
152 0 : if (!ccb) {
153 : /* should never happen but handle more gracefully */
154 0 : printf("%s: %s: too many ccbs queued\n",
155 0 : DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname);
156 0 : goto bad;
157 : }
158 0 : sr_wu_enqueue_ccb(wu, ccb);
159 :
160 0 : leftover -= length;
161 0 : if (leftover == 0)
162 : break;
163 0 : data += length;
164 0 : lbaoffs += length;
165 : }
166 :
167 0 : sr_schedule_wu(wu);
168 :
169 0 : return (0);
170 :
171 : bad:
172 : /* wu is unwound by sr_wu_put */
173 0 : return (1);
174 0 : }
|