dm_target_stripe.c revision 1.5.2.6 1 /*$NetBSD: dm_target_stripe.c,v 1.5.2.6 2010/08/11 22:53:21 yamt 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
42 #include "dm.h"
43
44 #ifdef DM_TARGET_MODULE
45 /*
46 * Every target can be compiled directly to dm driver or as a
47 * separate module this part of target is used for loading targets
48 * to dm driver.
49 * Target can be unloaded from kernel only if there are no users of
50 * it e.g. there are no devices which uses that target.
51 */
52 #include <sys/kernel.h>
53 #include <sys/module.h>
54
55 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
56
57 static int
58 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
59 {
60 dm_target_t *dmt;
61 int r;
62 dmt = NULL;
63
64 switch (cmd) {
65 case MODULE_CMD_INIT:
66 if ((dmt = dm_target_lookup("stripe")) != NULL) {
67 dm_target_unbusy(dmt);
68 return EEXIST;
69 }
70 dmt = dm_target_alloc("stripe");
71
72 dmt->version[0] = 1;
73 dmt->version[1] = 0;
74 dmt->version[2] = 0;
75 strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
76 dmt->init = &dm_target_stripe_init;
77 dmt->status = &dm_target_stripe_status;
78 dmt->strategy = &dm_target_stripe_strategy;
79 dmt->sync = &dm_target_stripe_sync;
80 dmt->deps = &dm_target_stripe_deps;
81 dmt->destroy = &dm_target_stripe_destroy;
82 dmt->upcall = &dm_target_stripe_upcall;
83
84 r = dm_target_insert(dmt);
85
86 break;
87
88 case MODULE_CMD_FINI:
89 r = dm_target_rem("stripe");
90 break;
91
92 case MODULE_CMD_STAT:
93 return ENOTTY;
94
95 default:
96 return ENOTTY;
97 }
98
99 return r;
100 }
101 #endif
102
103 /*
104 * Init function called from dm_table_load_ioctl.
105 * Example line sent to dm from lvm tools when using striped target.
106 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
107 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
108 */
109 int
110 dm_target_stripe_init(dm_dev_t * dmv, void **target_config, char *params)
111 {
112 dm_target_stripe_config_t *tsc;
113 size_t len;
114 char **ap, *argv[10];
115
116 if (params == NULL)
117 return EINVAL;
118
119 len = strlen(params) + 1;
120
121 /*
122 * Parse a string, containing tokens delimited by white space,
123 * into an argument vector
124 */
125 for (ap = argv; ap < &argv[9] &&
126 (*ap = strsep(¶ms, " \t")) != NULL;) {
127 if (**ap != '\0')
128 ap++;
129 }
130
131 printf("Stripe target init function called!!\n");
132
133 printf("Stripe target chunk size %s number of stripes %s\n", argv[1], argv[0]);
134 printf("Stripe target device name %s -- offset %s\n", argv[2], argv[3]);
135 printf("Stripe target device name %s -- offset %s\n", argv[4], argv[5]);
136
137 if (atoi(argv[0]) > MAX_STRIPES)
138 return ENOTSUP;
139
140 if ((tsc = kmem_alloc(sizeof(dm_target_stripe_config_t), KM_NOSLEEP))
141 == NULL)
142 return ENOMEM;
143
144 /* Insert dmp to global pdev list */
145 if ((tsc->stripe_devs[0].pdev = dm_pdev_insert(argv[2])) == NULL)
146 return ENOENT;
147
148 /* Insert dmp to global pdev list */
149 if ((tsc->stripe_devs[1].pdev = dm_pdev_insert(argv[4])) == NULL)
150 return ENOENT;
151
152 tsc->stripe_devs[0].offset = atoi(argv[3]);
153 tsc->stripe_devs[1].offset = atoi(argv[5]);
154
155 /* Save length of param string */
156 tsc->params_len = len;
157 tsc->stripe_chunksize = atoi(argv[1]);
158 tsc->stripe_num = (uint8_t) atoi(argv[0]);
159
160 *target_config = tsc;
161
162 dmv->dev_type = DM_STRIPE_DEV;
163
164 return 0;
165 }
166 /* Status routine called to get params string. */
167 char *
168 dm_target_stripe_status(void *target_config)
169 {
170 dm_target_stripe_config_t *tsc;
171 char *params;
172
173 tsc = target_config;
174
175 if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL)
176 return NULL;
177
178 snprintf(params, DM_MAX_PARAMS_SIZE, "%d %" PRIu64 " %s %" PRIu64 " %s %" PRIu64,
179 tsc->stripe_num, tsc->stripe_chunksize,
180 tsc->stripe_devs[0].pdev->name, tsc->stripe_devs[0].offset,
181 tsc->stripe_devs[1].pdev->name, tsc->stripe_devs[1].offset);
182
183 return params;
184 }
185 /* Strategy routine called from dm_strategy. */
186 int
187 dm_target_stripe_strategy(dm_table_entry_t * table_en, struct buf * bp)
188 {
189 dm_target_stripe_config_t *tsc;
190 struct buf *nestbuf;
191 uint64_t blkno, blkoff;
192 uint64_t stripe, stripe_blknr;
193 uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
194 int stripe_devnr;
195
196 tsc = table_en->target_config;
197 if (tsc == NULL)
198 return 0;
199
200 /* printf("Stripe target read function called %" PRIu64 "!!\n",
201 tlc->offset);*/
202
203 /* calculate extent of request */
204 KASSERT(bp->b_resid % DEV_BSIZE == 0);
205
206 blkno = bp->b_blkno;
207 blkoff = 0;
208 num_blks = bp->b_resid / DEV_BSIZE;
209 for (;;) {
210 /* blockno to strip piece nr */
211 stripe = blkno / tsc->stripe_chunksize;
212 stripe_off = blkno % tsc->stripe_chunksize;
213
214 /* where we are inside the strip */
215 stripe_devnr = stripe % tsc->stripe_num;
216 stripe_blknr = stripe / tsc->stripe_num;
217
218 /* how much is left before we hit a boundary */
219 stripe_rest = tsc->stripe_chunksize - stripe_off;
220
221 /* issue this piece on stripe `stripe' */
222 issue_blks = MIN(stripe_rest, num_blks);
223 nestbuf = getiobuf(NULL, true);
224
225 nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
226 nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
227 nestbuf->b_blkno += tsc->stripe_devs[stripe_devnr].offset;
228
229 VOP_STRATEGY(tsc->stripe_devs[stripe_devnr].pdev->pdev_vnode, nestbuf);
230
231 blkno += issue_blks;
232 blkoff += issue_blks * DEV_BSIZE;
233 num_blks -= issue_blks;
234
235 if (num_blks <= 0)
236 break;
237 }
238
239 return 0;
240 }
241 /* Sync underlying disk caches. */
242 int
243 dm_target_stripe_sync(dm_table_entry_t * table_en)
244 {
245 int cmd, err, i;
246 dm_target_stripe_config_t *tsc;
247
248 tsc = table_en->target_config;
249
250 err = 0;
251 cmd = 1;
252
253 for (i = 0; i < tsc->stripe_num; i++) {
254 if ((err = VOP_IOCTL(tsc->stripe_devs[i].pdev->pdev_vnode, DIOCCACHESYNC,
255 &cmd, FREAD|FWRITE, kauth_cred_get())) != 0)
256 return err;
257 }
258
259 return err;
260
261 }
262 /* Destroy target specific data. */
263 int
264 dm_target_stripe_destroy(dm_table_entry_t * table_en)
265 {
266 dm_target_stripe_config_t *tsc;
267
268 tsc = table_en->target_config;
269
270 if (tsc == NULL)
271 return 0;
272
273 dm_pdev_decr(tsc->stripe_devs[0].pdev);
274 dm_pdev_decr(tsc->stripe_devs[1].pdev);
275
276 /* Unbusy target so we can unload it */
277 dm_target_unbusy(table_en->target);
278
279 kmem_free(tsc, sizeof(dm_target_stripe_config_t));
280
281 table_en->target_config = NULL;
282
283 return 0;
284 }
285 /* Doesn't not need to do anything here. */
286 int
287 dm_target_stripe_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
288 {
289 dm_target_stripe_config_t *tsc;
290 struct vattr va;
291
292 int error;
293
294 if (table_en->target_config == NULL)
295 return ENOENT;
296
297 tsc = table_en->target_config;
298
299 if ((error = VOP_GETATTR(tsc->stripe_devs[0].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
300 return error;
301
302 prop_array_add_uint64(prop_array, (uint64_t) va.va_rdev);
303
304 if ((error = VOP_GETATTR(tsc->stripe_devs[1].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
305 return error;
306
307 prop_array_add_uint64(prop_array, (uint64_t) va.va_rdev);
308
309 return 0;
310 }
311 /* Unsupported for this target. */
312 int
313 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)
314 {
315 return 0;
316 }
317