dm_target_stripe.c revision 1.20 1 /*$NetBSD: dm_target_stripe.c,v 1.20 2014/08/18 17:16:42 agc Exp $*/
2
3 /*
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * This file implements initial version of device-mapper stripe target.
34 */
35 #include <sys/types.h>
36 #include <sys/param.h>
37
38 #include <sys/buf.h>
39 #include <sys/kmem.h>
40 #include <sys/vnode.h>
41 #include <sys/lwp.h>
42
43 #include "dm.h"
44
45 #ifdef DM_TARGET_MODULE
46 /*
47 * Every target can be compiled directly to dm driver or as a
48 * separate module this part of target is used for loading targets
49 * to dm driver.
50 * Target can be unloaded from kernel only if there are no users of
51 * it e.g. there are no devices which uses that target.
52 */
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55
56 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
57
58 static int
59 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
60 {
61 dm_target_t *dmt;
62 int r;
63 dmt = NULL;
64
65 switch (cmd) {
66 case MODULE_CMD_INIT:
67 if ((dmt = dm_target_lookup("stripe")) != NULL) {
68 dm_target_unbusy(dmt);
69 return EEXIST;
70 }
71 dmt = dm_target_alloc("stripe");
72
73 dmt->version[0] = 1;
74 dmt->version[1] = 0;
75 dmt->version[2] = 0;
76 strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
77 dmt->init = &dm_target_stripe_init;
78 dmt->status = &dm_target_stripe_status;
79 dmt->strategy = &dm_target_stripe_strategy;
80 dmt->sync = &dm_target_stripe_sync;
81 dmt->deps = &dm_target_stripe_deps;
82 dmt->destroy = &dm_target_stripe_destroy;
83 dmt->upcall = &dm_target_stripe_upcall;
84 dmt->secsize = &dm_target_stripe_secsize;
85
86 r = dm_target_insert(dmt);
87
88 break;
89
90 case MODULE_CMD_FINI:
91 r = dm_target_rem("stripe");
92 break;
93
94 case MODULE_CMD_STAT:
95 return ENOTTY;
96
97 default:
98 return ENOTTY;
99 }
100
101 return r;
102 }
103 #endif
104
105 /*
106 * Init function called from dm_table_load_ioctl.
107 * DM_STRIPE_DEV_OFFSET should always hold the index of the first device-offset
108 * pair in the parameters.
109 * Example line sent to dm from lvm tools when using striped target.
110 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
111 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
112 */
113 int
114 dm_target_stripe_init(dm_dev_t * dmv, void **target_config, char *params)
115 {
116 dm_target_linear_config_t *tlc;
117 dm_target_stripe_config_t *tsc;
118 size_t len;
119 char **ap, *argv[10];
120 int strpc, strpi;
121
122 if (params == NULL)
123 return EINVAL;
124
125 len = strlen(params) + 1;
126
127 /*
128 * Parse a string, containing tokens delimited by white space,
129 * into an argument vector
130 */
131 for (ap = argv; ap <= &argv[9] &&
132 (*ap = strsep(¶ms, " \t")) != NULL;) {
133 if (**ap != '\0')
134 ap++;
135 }
136
137 printf("Stripe target init function called!!\n");
138
139 printf("Stripe target chunk size %s number of stripes %s\n",
140 argv[1], argv[0]);
141
142 if ((tsc = kmem_alloc(sizeof(*tsc), KM_NOSLEEP)) == NULL)
143 return ENOMEM;
144
145 /* Initialize linked list for striping devices */
146 TAILQ_INIT(&tsc->stripe_devs);
147
148 /* Save length of param string */
149 tsc->params_len = len;
150 tsc->stripe_chunksize = atoi(argv[1]);
151 tsc->stripe_num = (uint8_t) atoi(argv[0]);
152
153 strpc = DM_STRIPE_DEV_OFFSET + (tsc->stripe_num * 2);
154 for (strpi = DM_STRIPE_DEV_OFFSET; strpi < strpc; strpi += 2) {
155 printf("Stripe target device name %s -- offset %s\n",
156 argv[strpi], argv[strpi+1]);
157
158 tlc = kmem_alloc(sizeof(*tlc), KM_NOSLEEP);
159 if ((tlc->pdev = dm_pdev_insert(argv[strpi])) == NULL) {
160 kmem_free(tsc, sizeof(*tsc));
161 kmem_free(tlc, sizeof(*tlc));
162 return ENOENT;
163 }
164 tlc->offset = atoi(argv[strpi+1]);
165
166 /* Insert striping device to linked list. */
167 TAILQ_INSERT_TAIL(&tsc->stripe_devs, tlc, entries);
168 }
169
170 *target_config = tsc;
171
172 dmv->dev_type = DM_STRIPE_DEV;
173
174 return 0;
175 }
176 /* Status routine called to get params string. */
177 char *
178 dm_target_stripe_status(void *target_config)
179 {
180 dm_target_linear_config_t *tlc;
181 dm_target_stripe_config_t *tsc;
182 char *params, *tmp;
183
184 tsc = target_config;
185
186 if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL)
187 return NULL;
188
189 if ((tmp = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL) {
190 kmem_free(params, DM_MAX_PARAMS_SIZE);
191 return NULL;
192 }
193
194 snprintf(params, DM_MAX_PARAMS_SIZE, "%d %" PRIu64,
195 tsc->stripe_num, tsc->stripe_chunksize);
196
197 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
198 snprintf(tmp, DM_MAX_PARAMS_SIZE, " %s %" PRIu64,
199 tlc->pdev->name, tlc->offset);
200 strcat(params, tmp);
201 }
202
203 kmem_free(tmp, DM_MAX_PARAMS_SIZE);
204
205 return params;
206 }
207 /* Strategy routine called from dm_strategy. */
208 int
209 dm_target_stripe_strategy(dm_table_entry_t * table_en, struct buf * bp)
210 {
211 dm_target_linear_config_t *tlc;
212 dm_target_stripe_config_t *tsc;
213 struct buf *nestbuf;
214 uint64_t blkno, blkoff;
215 uint64_t stripe, stripe_blknr;
216 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
217 int i, stripe_devnr;
218
219 tsc = table_en->target_config;
220 if (tsc == NULL)
221 return 0;
222
223 /* printf("Stripe target read function called %" PRIu64 "!!\n",
224 tlc->offset);*/
225
226 /* calculate extent of request */
227 KASSERT(bp->b_resid % DEV_BSIZE == 0);
228
229 blkno = bp->b_blkno;
230 blkoff = 0;
231 num_blks = bp->b_resid / DEV_BSIZE;
232 for (;;) {
233 /* blockno to strip piece nr */
234 stripe = blkno / tsc->stripe_chunksize;
235 stripe_off = blkno % tsc->stripe_chunksize;
236
237 /* where we are inside the strip */
238 stripe_devnr = stripe % tsc->stripe_num;
239 stripe_blknr = stripe / tsc->stripe_num;
240
241 /* how much is left before we hit a boundary */
242 stripe_rest = tsc->stripe_chunksize - stripe_off;
243
244 /* issue this piece on stripe `stripe' */
245 issue_blks = MIN(stripe_rest, num_blks);
246 nestbuf = getiobuf(NULL, true);
247
248 nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
249 nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
250
251 tlc = TAILQ_FIRST(&tsc->stripe_devs);
252 for (i = 0; i < stripe_devnr && tlc != NULL; i++)
253 tlc = TAILQ_NEXT(tlc, entries);
254
255 /* by this point we should have an tlc */
256 KASSERT(tlc != NULL);
257
258 nestbuf->b_blkno += tlc->offset;
259
260 VOP_STRATEGY(tlc->pdev->pdev_vnode, nestbuf);
261
262 blkno += issue_blks;
263 blkoff += issue_blks * DEV_BSIZE;
264 num_blks -= issue_blks;
265
266 if (num_blks <= 0)
267 break;
268 }
269
270 return 0;
271 }
272 /* Sync underlying disk caches. */
273 int
274 dm_target_stripe_sync(dm_table_entry_t * table_en)
275 {
276 int cmd, err;
277 dm_target_stripe_config_t *tsc;
278 dm_target_linear_config_t *tlc;
279
280 tsc = table_en->target_config;
281
282 err = 0;
283 cmd = 1;
284
285 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
286 if ((err = VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC,
287 &cmd, FREAD|FWRITE, kauth_cred_get())) != 0)
288 return err;
289 }
290
291 return err;
292
293 }
294 /* Destroy target specific data. */
295 int
296 dm_target_stripe_destroy(dm_table_entry_t * table_en)
297 {
298 dm_target_stripe_config_t *tsc;
299 dm_target_linear_config_t *tlc;
300
301 tsc = table_en->target_config;
302
303 if (tsc == NULL)
304 return 0;
305
306 while ((tlc = TAILQ_FIRST(&tsc->stripe_devs)) != NULL) {
307 TAILQ_REMOVE(&tsc->stripe_devs, tlc, entries);
308 dm_pdev_decr(tlc->pdev);
309 kmem_free(tlc, sizeof(*tlc));
310 }
311
312 /* Unbusy target so we can unload it */
313 dm_target_unbusy(table_en->target);
314
315 kmem_free(tsc, sizeof(*tsc));
316
317 table_en->target_config = NULL;
318
319 return 0;
320 }
321 /* Doesn't not need to do anything here. */
322 int
323 dm_target_stripe_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
324 {
325 dm_target_stripe_config_t *tsc;
326 dm_target_linear_config_t *tlc;
327
328 if (table_en->target_config == NULL)
329 return ENOENT;
330
331 tsc = table_en->target_config;
332
333 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
334 prop_array_add_uint64(prop_array,
335 (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
336 }
337
338 return 0;
339 }
340 /* Unsupported for this target. */
341 int
342 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)
343 {
344 return 0;
345 }
346 /*
347 * Compute physical block size
348 * For a stripe target we chose the maximum sector size of all
349 * stripe devices. For the supported power-of-2 sizes this is equivalent
350 * to the least common multiple.
351 */
352 int
353 dm_target_stripe_secsize(dm_table_entry_t * table_en, unsigned *secsizep)
354 {
355 dm_target_linear_config_t *tlc;
356 dm_target_stripe_config_t *tsc;
357 unsigned secsize;
358
359 secsize = 0;
360
361 tsc = table_en->target_config;
362 if (tsc != NULL) {
363 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
364 if (secsize < tlc->pdev->pdev_secsize)
365 secsize = tlc->pdev->pdev_secsize;
366 }
367 }
368
369 *secsizep = secsize;
370
371 return 0;
372 }
373