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