dm_target_stripe.c revision 1.34 1 /*$NetBSD: dm_target_stripe.c,v 1.34 2019/12/12 16:28:24 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.34 2019/12/12 16:28:24 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 typedef struct target_stripe_config {
47 #define DM_STRIPE_DEV_OFFSET 2
48 struct target_linear_devs stripe_devs;
49 uint8_t stripe_num;
50 uint64_t stripe_chunksize;
51 } dm_target_stripe_config_t;
52
53 #ifdef DM_TARGET_MODULE
54 /*
55 * Every target can be compiled directly to dm driver or as a
56 * separate module this part of target is used for loading targets
57 * to dm driver.
58 * Target can be unloaded from kernel only if there are no users of
59 * it e.g. there are no devices which uses that target.
60 */
61 #include <sys/kernel.h>
62 #include <sys/module.h>
63
64 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
65
66 static int
67 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
68 {
69 dm_target_t *dmt;
70 int r;
71 dmt = NULL;
72
73 switch (cmd) {
74 case MODULE_CMD_INIT:
75 if ((dmt = dm_target_lookup("striped")) != NULL) {
76 dm_target_unbusy(dmt);
77 return EEXIST;
78 }
79 dmt = dm_target_alloc("striped");
80
81 dmt->version[0] = 1;
82 dmt->version[1] = 0;
83 dmt->version[2] = 0;
84 dmt->init = &dm_target_stripe_init;
85 dmt->status = &dm_target_stripe_status;
86 dmt->strategy = &dm_target_stripe_strategy;
87 dmt->sync = &dm_target_stripe_sync;
88 dmt->deps = &dm_target_stripe_deps;
89 dmt->destroy = &dm_target_stripe_destroy;
90 dmt->upcall = &dm_target_stripe_upcall;
91 dmt->secsize = &dm_target_stripe_secsize;
92
93 r = dm_target_insert(dmt);
94
95 break;
96
97 case MODULE_CMD_FINI:
98 r = dm_target_rem("striped");
99 break;
100
101 case MODULE_CMD_STAT:
102 return ENOTTY;
103
104 default:
105 return ENOTTY;
106 }
107
108 return r;
109 }
110 #endif
111
112 static void
113 dm_target_stripe_fini(dm_target_stripe_config_t *tsc)
114 {
115 dm_target_linear_config_t *tlc;
116
117 if (tsc == NULL)
118 return;
119
120 while ((tlc = TAILQ_FIRST(&tsc->stripe_devs)) != NULL) {
121 TAILQ_REMOVE(&tsc->stripe_devs, tlc, entries);
122 dm_pdev_decr(tlc->pdev);
123 kmem_free(tlc, sizeof(*tlc));
124 }
125
126 kmem_free(tsc, sizeof(*tsc));
127 }
128
129 /*
130 * Init function called from dm_table_load_ioctl.
131 * DM_STRIPE_DEV_OFFSET should always hold the index of the first device-offset
132 * pair in the parameters.
133 * Example line sent to dm from lvm tools when using striped target.
134 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
135 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
136 */
137 int
138 dm_target_stripe_init(dm_table_entry_t *table_en, int argc, char **argv)
139 {
140 dm_target_linear_config_t *tlc;
141 dm_target_stripe_config_t *tsc;
142 int strpc, strpi;
143
144 /*
145 if (argc < 4) {
146 printf("Stripe target takes 4 or more args\n");
147 return EINVAL;
148 }
149 */
150
151 printf("Stripe target init function called!!\n");
152 printf("Stripe target chunk size %s number of stripes %s\n",
153 argv[1], argv[0]);
154
155 tsc = kmem_alloc(sizeof(*tsc), KM_SLEEP);
156
157 /* Initialize linked list for striping devices */
158 TAILQ_INIT(&tsc->stripe_devs);
159
160 /* Save length of param string */
161 tsc->stripe_chunksize = atoi(argv[1]);
162 tsc->stripe_num = (uint8_t) atoi(argv[0]);
163
164 strpc = DM_STRIPE_DEV_OFFSET + (tsc->stripe_num * 2);
165 for (strpi = DM_STRIPE_DEV_OFFSET; strpi < strpc; strpi += 2) {
166 printf("Stripe target device name %s -- offset %s\n",
167 argv[strpi], argv[strpi+1]);
168
169 tlc = kmem_alloc(sizeof(*tlc), KM_SLEEP);
170 if ((tlc->pdev = dm_pdev_insert(argv[strpi])) == NULL) {
171 kmem_free(tlc, sizeof(*tlc));
172 dm_target_stripe_fini(tsc);
173 return ENOENT;
174 }
175 tlc->offset = atoi(argv[strpi+1]);
176
177 /* Insert striping device to linked list. */
178 TAILQ_INSERT_TAIL(&tsc->stripe_devs, tlc, entries);
179 }
180
181 table_en->target_config = tsc;
182
183 return 0;
184 }
185
186 /* Status routine called to get params string. */
187 char *
188 dm_target_stripe_status(void *target_config)
189 {
190 dm_target_linear_config_t *tlc;
191 dm_target_stripe_config_t *tsc;
192 char *params, *tmp;
193
194 tsc = target_config;
195
196 params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
197 tmp = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
198
199 snprintf(params, DM_MAX_PARAMS_SIZE, "%d %" PRIu64,
200 tsc->stripe_num, tsc->stripe_chunksize);
201
202 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
203 snprintf(tmp, DM_MAX_PARAMS_SIZE, " %s %" PRIu64,
204 tlc->pdev->name, tlc->offset);
205 strcat(params, tmp);
206 }
207
208 kmem_free(tmp, DM_MAX_PARAMS_SIZE);
209
210 return params;
211 }
212
213 /* Strategy routine called from dm_strategy. */
214 int
215 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
216 {
217 dm_target_linear_config_t *tlc;
218 dm_target_stripe_config_t *tsc;
219 struct buf *nestbuf;
220 uint64_t blkno, blkoff;
221 uint64_t stripe, stripe_blknr;
222 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
223 int i, stripe_devnr;
224
225 tsc = table_en->target_config;
226 if (tsc == NULL)
227 return 0;
228
229 /* printf("Stripe target read function called %" PRIu64 "!!\n",
230 tlc->offset);*/
231
232 /* calculate extent of request */
233 KASSERT(bp->b_resid % DEV_BSIZE == 0);
234
235 blkno = bp->b_blkno;
236 blkoff = 0;
237 num_blks = bp->b_resid / DEV_BSIZE;
238 for (;;) {
239 /* blockno to stripe piece nr */
240 stripe = blkno / tsc->stripe_chunksize;
241 stripe_off = blkno % tsc->stripe_chunksize;
242
243 /* where we are inside the stripe */
244 stripe_devnr = stripe % tsc->stripe_num;
245 stripe_blknr = stripe / tsc->stripe_num;
246
247 /* how much is left before we hit a boundary */
248 stripe_rest = tsc->stripe_chunksize - stripe_off;
249
250 /* issue this piece on stripe `stripe' */
251 issue_blks = MIN(stripe_rest, num_blks);
252 nestbuf = getiobuf(NULL, true);
253
254 nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
255 nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
256
257 tlc = TAILQ_FIRST(&tsc->stripe_devs);
258 for (i = 0; i < stripe_devnr && tlc != NULL; i++)
259 tlc = TAILQ_NEXT(tlc, entries);
260
261 /* by this point we should have an tlc */
262 KASSERT(tlc != NULL);
263
264 nestbuf->b_blkno += tlc->offset;
265
266 VOP_STRATEGY(tlc->pdev->pdev_vnode, nestbuf);
267
268 blkno += issue_blks;
269 blkoff += issue_blks * DEV_BSIZE;
270 num_blks -= issue_blks;
271
272 if (num_blks <= 0)
273 break;
274 }
275
276 return 0;
277 }
278
279 /* Sync underlying disk caches. */
280 int
281 dm_target_stripe_sync(dm_table_entry_t *table_en)
282 {
283 int cmd, err;
284 dm_target_stripe_config_t *tsc;
285 dm_target_linear_config_t *tlc;
286
287 tsc = table_en->target_config;
288
289 err = 0;
290 cmd = 1;
291
292 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
293 if ((err = VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC,
294 &cmd, FREAD|FWRITE, kauth_cred_get())) != 0)
295 return err;
296 }
297
298 return err;
299
300 }
301
302 /* Destroy target specific data. */
303 int
304 dm_target_stripe_destroy(dm_table_entry_t *table_en)
305 {
306 dm_target_stripe_fini(table_en->target_config);
307
308 /* Unbusy target so we can unload it */
309 dm_target_unbusy(table_en->target);
310
311 return 0;
312 }
313
314 /* Doesn't not need to do anything here. */
315 int
316 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
317 {
318 dm_target_stripe_config_t *tsc;
319 dm_target_linear_config_t *tlc;
320
321 if (table_en->target_config == NULL)
322 return ENOENT;
323
324 tsc = table_en->target_config;
325
326 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
327 prop_array_add_uint64(prop_array,
328 (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
329 }
330
331 return 0;
332 }
333
334 /* Unsupported for this target. */
335 int
336 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
337 {
338 return 0;
339 }
340
341 /*
342 * Compute physical block size
343 * For a stripe target we chose the maximum sector size of all
344 * stripe devices. For the supported power-of-2 sizes this is equivalent
345 * to the least common multiple.
346 */
347 int
348 dm_target_stripe_secsize(dm_table_entry_t *table_en, unsigned *secsizep)
349 {
350 dm_target_linear_config_t *tlc;
351 dm_target_stripe_config_t *tsc;
352 unsigned secsize;
353
354 secsize = 0;
355
356 tsc = table_en->target_config;
357 if (tsc != NULL) {
358 TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
359 if (secsize < tlc->pdev->pdev_secsize)
360 secsize = tlc->pdev->pdev_secsize;
361 }
362 }
363
364 *secsizep = secsize;
365
366 return 0;
367 }
368