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