dm_target_stripe.c revision 1.30 1 /*$NetBSD: dm_target_stripe.c,v 1.30 2019/12/07 15:28:39 tkusumi 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 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: dm_target_stripe.c,v 1.30 2019/12/07 15:28:39 tkusumi Exp $");
33
34 /*
35 * This file implements initial version of device-mapper stripe target.
36 */
37 #include <sys/types.h>
38 #include <sys/param.h>
39
40 #include <sys/buf.h>
41 #include <sys/kmem.h>
42 #include <sys/lwp.h>
43
44 #include "dm.h"
45
46 #ifdef DM_TARGET_MODULE
47 /*
48 * Every target can be compiled directly to dm driver or as a
49 * separate module this part of target is used for loading targets
50 * to dm driver.
51 * Target can be unloaded from kernel only if there are no users of
52 * it e.g. there are no devices which uses that target.
53 */
54 #include <sys/kernel.h>
55 #include <sys/module.h>
56
57 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
58
59 static int
60 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
61 {
62 dm_target_t *dmt;
63 int r;
64 dmt = NULL;
65
66 switch (cmd) {
67 case MODULE_CMD_INIT:
68 if ((dmt = dm_target_lookup("stripe")) != NULL) {
69 dm_target_unbusy(dmt);
70 return EEXIST;
71 }
72 dmt = dm_target_alloc("stripe");
73
74 dmt->version[0] = 1;
75 dmt->version[1] = 0;
76 dmt->version[2] = 0;
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 static void
106 dm_target_stripe_fini(dm_target_stripe_config_t *tsc)
107 {
108 dm_target_linear_config_t *tlc;
109
110 if (tsc == NULL)
111 return;
112
113 while ((tlc = TAILQ_FIRST(&tsc->stripe_devs)) != NULL) {
114 TAILQ_REMOVE(&tsc->stripe_devs, tlc, entries);
115 dm_pdev_decr(tlc->pdev);
116 kmem_free(tlc, sizeof(*tlc));
117 }
118
119 kmem_free(tsc, sizeof(*tsc));
120 }
121
122 /*
123 * Init function called from dm_table_load_ioctl.
124 * DM_STRIPE_DEV_OFFSET should always hold the index of the first device-offset
125 * pair in the parameters.
126 * Example line sent to dm from lvm tools when using striped target.
127 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
128 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
129 */
130 int
131 dm_target_stripe_init(dm_dev_t *dmv, void **target_config, char *params)
132 {
133 dm_target_linear_config_t *tlc;
134 dm_target_stripe_config_t *tsc;
135 size_t len;
136 char **ap, *argv[10];
137 int strpc, strpi;
138
139 if (params == NULL)
140 return EINVAL;
141
142 len = strlen(params) + 1;
143
144 /*
145 * Parse a string, containing tokens delimited by white space,
146 * into an argument vector
147 */
148 for (ap = argv; ap <= &argv[9] &&
149 (*ap = strsep(¶ms, " \t")) != NULL;) {
150 if (**ap != '\0')
151 ap++;
152 }
153
154 printf("Stripe target init function called!!\n");
155
156 printf("Stripe target chunk size %s number of stripes %s\n",
157 argv[1], argv[0]);
158
159 tsc = kmem_alloc(sizeof(*tsc), KM_SLEEP);
160
161 /* Initialize linked list for striping devices */
162 TAILQ_INIT(&tsc->stripe_devs);
163
164 /* Save length of param string */
165 tsc->params_len = len;
166 tsc->stripe_chunksize = atoi(argv[1]);
167 tsc->stripe_num = (uint8_t) atoi(argv[0]);
168
169 strpc = DM_STRIPE_DEV_OFFSET + (tsc->stripe_num * 2);
170 for (strpi = DM_STRIPE_DEV_OFFSET; strpi < strpc; strpi += 2) {
171 printf("Stripe target device name %s -- offset %s\n",
172 argv[strpi], argv[strpi+1]);
173
174 tlc = kmem_alloc(sizeof(*tlc), KM_SLEEP);
175 if ((tlc->pdev = dm_pdev_insert(argv[strpi])) == NULL) {
176 kmem_free(tlc, sizeof(*tlc));
177 dm_target_stripe_fini(tsc);
178 return ENOENT;
179 }
180 tlc->offset = atoi(argv[strpi+1]);
181
182 /* Insert striping device to linked list. */
183 TAILQ_INSERT_TAIL(&tsc->stripe_devs, tlc, entries);
184 }
185
186 *target_config = tsc;
187
188 return 0;
189 }
190
191 /* Status routine called to get params string. */
192 char *
193 dm_target_stripe_status(void *target_config)
194 {
195 dm_target_linear_config_t *tlc;
196 dm_target_stripe_config_t *tsc;
197 char *params, *tmp;
198
199 tsc = target_config;
200
201 params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
202 tmp = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
203
204 snprintf(params, DM_MAX_PARAMS_SIZE, "%d %" PRIu64,
205 tsc->stripe_num, tsc->stripe_chunksize);
206
207 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
208 snprintf(tmp, DM_MAX_PARAMS_SIZE, " %s %" PRIu64,
209 tlc->pdev->name, tlc->offset);
210 strcat(params, tmp);
211 }
212
213 kmem_free(tmp, DM_MAX_PARAMS_SIZE);
214
215 return params;
216 }
217
218 /* Strategy routine called from dm_strategy. */
219 int
220 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
221 {
222 dm_target_linear_config_t *tlc;
223 dm_target_stripe_config_t *tsc;
224 struct buf *nestbuf;
225 uint64_t blkno, blkoff;
226 uint64_t stripe, stripe_blknr;
227 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
228 int i, stripe_devnr;
229
230 tsc = table_en->target_config;
231 if (tsc == NULL)
232 return 0;
233
234 /* printf("Stripe target read function called %" PRIu64 "!!\n",
235 tlc->offset);*/
236
237 /* calculate extent of request */
238 KASSERT(bp->b_resid % DEV_BSIZE == 0);
239
240 blkno = bp->b_blkno;
241 blkoff = 0;
242 num_blks = bp->b_resid / DEV_BSIZE;
243 for (;;) {
244 /* blockno to stripe piece nr */
245 stripe = blkno / tsc->stripe_chunksize;
246 stripe_off = blkno % tsc->stripe_chunksize;
247
248 /* where we are inside the stripe */
249 stripe_devnr = stripe % tsc->stripe_num;
250 stripe_blknr = stripe / tsc->stripe_num;
251
252 /* how much is left before we hit a boundary */
253 stripe_rest = tsc->stripe_chunksize - stripe_off;
254
255 /* issue this piece on stripe `stripe' */
256 issue_blks = MIN(stripe_rest, num_blks);
257 nestbuf = getiobuf(NULL, true);
258
259 nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
260 nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
261
262 tlc = TAILQ_FIRST(&tsc->stripe_devs);
263 for (i = 0; i < stripe_devnr && tlc != NULL; i++)
264 tlc = TAILQ_NEXT(tlc, entries);
265
266 /* by this point we should have an tlc */
267 KASSERT(tlc != NULL);
268
269 nestbuf->b_blkno += tlc->offset;
270
271 VOP_STRATEGY(tlc->pdev->pdev_vnode, nestbuf);
272
273 blkno += issue_blks;
274 blkoff += issue_blks * DEV_BSIZE;
275 num_blks -= issue_blks;
276
277 if (num_blks <= 0)
278 break;
279 }
280
281 return 0;
282 }
283
284 /* Sync underlying disk caches. */
285 int
286 dm_target_stripe_sync(dm_table_entry_t *table_en)
287 {
288 int cmd, err;
289 dm_target_stripe_config_t *tsc;
290 dm_target_linear_config_t *tlc;
291
292 tsc = table_en->target_config;
293
294 err = 0;
295 cmd = 1;
296
297 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
298 if ((err = VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC,
299 &cmd, FREAD|FWRITE, kauth_cred_get())) != 0)
300 return err;
301 }
302
303 return err;
304
305 }
306
307 /* Destroy target specific data. */
308 int
309 dm_target_stripe_destroy(dm_table_entry_t *table_en)
310 {
311 dm_target_stripe_fini(table_en->target_config);
312
313 /* Unbusy target so we can unload it */
314 dm_target_unbusy(table_en->target);
315
316 return 0;
317 }
318
319 /* Doesn't not need to do anything here. */
320 int
321 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
322 {
323 dm_target_stripe_config_t *tsc;
324 dm_target_linear_config_t *tlc;
325
326 if (table_en->target_config == NULL)
327 return ENOENT;
328
329 tsc = table_en->target_config;
330
331 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
332 prop_array_add_uint64(prop_array,
333 (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
334 }
335
336 return 0;
337 }
338
339 /* Unsupported for this target. */
340 int
341 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
342 {
343 return 0;
344 }
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