xref: /openbsd/sys/dev/softraid_concat.c (revision 264ca280)
1 /* $OpenBSD: softraid_concat.c,v 1.25 2016/04/12 16:26:54 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 	if (no_chunk < 2) {
64 		sr_error(sd->sd_sc, "%s requires two or more chunks",
65 		    sd->sd_name);
66 		return EINVAL;
67         }
68 
69 	return sr_concat_init(sd);
70 }
71 
72 int
73 sr_concat_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
74     int no_chunk, void *data)
75 {
76 	return sr_concat_init(sd);
77 }
78 
79 int
80 sr_concat_init(struct sr_discipline *sd)
81 {
82 	sd->sd_max_ccb_per_wu = SR_CONCAT_NOWU * sd->sd_meta->ssdi.ssd_chunk_no;
83 
84 	return 0;
85 }
86 
87 int
88 sr_concat_rw(struct sr_workunit *wu)
89 {
90 	struct sr_discipline	*sd = wu->swu_dis;
91 	struct scsi_xfer	*xs = wu->swu_xs;
92 	struct sr_ccb		*ccb;
93 	struct sr_chunk		*scp;
94 	daddr_t			blkno;
95 	int64_t			lbaoffs, offset;
96 	int64_t			no_chunk, chunkend, chunk, chunksize;
97 	int64_t			length, leftover;
98 	u_int8_t		*data;
99 
100 	/* blkno and scsi error will be handled by sr_validate_io */
101 	if (sr_validate_io(wu, &blkno, "sr_concat_rw"))
102 		goto bad;
103 
104 	no_chunk = sd->sd_meta->ssdi.ssd_chunk_no;
105 
106 	DNPRINTF(SR_D_DIS, "%s: %s: front end io: blkno %lld size %d\n",
107 	    DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname,
108 	    (long long)blkno, xs->datalen);
109 
110 	/* All offsets are in bytes. */
111 	lbaoffs = blkno << DEV_BSHIFT;
112 	leftover = xs->datalen;
113 	data = xs->data;
114 	for (;;) {
115 
116 		chunkend = 0;
117 		offset = lbaoffs;
118 		for (chunk = 0; chunk < no_chunk; chunk++) {
119 			chunksize = sd->sd_vol.sv_chunks[chunk]->src_size <<
120 			    DEV_BSHIFT;
121 			chunkend += chunksize;
122 			if (lbaoffs < chunkend)
123 				break;
124 			offset -= chunksize;
125 		}
126 		if (lbaoffs > chunkend)
127 			goto bad;
128 
129 		length = MIN(MIN(leftover, chunkend - lbaoffs), MAXPHYS);
130 
131 		/* make sure chunk is online */
132 		scp = sd->sd_vol.sv_chunks[chunk];
133 		if (scp->src_meta.scm_status != BIOC_SDONLINE)
134 			goto bad;
135 
136 		DNPRINTF(SR_D_DIS, "%s: %s %s io lbaoffs %lld "
137 		    "chunk %lld chunkend %lld offset %lld length %lld "
138 		    "leftover %lld data %p\n",
139 		    DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, sd->sd_name,
140 		    lbaoffs, chunk, chunkend, offset, length, leftover, data);
141 
142 		blkno = offset >> DEV_BSHIFT;
143 		ccb = sr_ccb_rw(sd, chunk, blkno, length, data, xs->flags, 0);
144 		if (!ccb) {
145 			/* should never happen but handle more gracefully */
146 			printf("%s: %s: too many ccbs queued\n",
147 			    DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname);
148 			goto bad;
149 		}
150 		sr_wu_enqueue_ccb(wu, ccb);
151 
152 		leftover -= length;
153 		if (leftover == 0)
154 			break;
155 		data += length;
156 		lbaoffs += length;
157 	}
158 
159 	sr_schedule_wu(wu);
160 
161 	return (0);
162 
163 bad:
164 	/* wu is unwound by sr_wu_put */
165 	return (1);
166 }
167