dm_target_stripe.c revision 1.2.6.2 1 1.2.6.2 jym /*$NetBSD: dm_target_stripe.c,v 1.2.6.2 2009/07/23 23:31:46 jym Exp $*/
2 1.1 haad
3 1.1 haad /*
4 1.1 haad * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.1 haad * All rights reserved.
6 1.1 haad *
7 1.1 haad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 haad * by Adam Hamsik.
9 1.1 haad *
10 1.1 haad * Redistribution and use in source and binary forms, with or without
11 1.1 haad * modification, are permitted provided that the following conditions
12 1.1 haad * are met:
13 1.1 haad * 1. Redistributions of source code must retain the above copyright
14 1.1 haad * notice, this list of conditions and the following disclaimer.
15 1.1 haad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 haad * notice, this list of conditions and the following disclaimer in the
17 1.1 haad * documentation and/or other materials provided with the distribution.
18 1.1 haad *
19 1.1 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 haad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 haad */
31 1.1 haad
32 1.1 haad /*
33 1.1 haad * This file implements initial version of device-mapper stripe target.
34 1.1 haad */
35 1.1 haad #include <sys/types.h>
36 1.1 haad #include <sys/param.h>
37 1.1 haad
38 1.1 haad #include <sys/buf.h>
39 1.2.6.1 jym #include <sys/kmem.h>
40 1.2.6.1 jym #include <sys/vnode.h>
41 1.1 haad
42 1.1 haad #include "dm.h"
43 1.2.6.2 jym #include "netbsd-dm.h"
44 1.1 haad
45 1.1 haad #ifdef DM_TARGET_MODULE
46 1.1 haad /*
47 1.1 haad * Every target can be compiled directly to dm driver or as a
48 1.1 haad * separate module this part of target is used for loading targets
49 1.1 haad * to dm driver.
50 1.1 haad * Target can be unloaded from kernel only if there are no users of
51 1.1 haad * it e.g. there are no devices which uses that target.
52 1.1 haad */
53 1.1 haad #include <sys/kernel.h>
54 1.1 haad #include <sys/module.h>
55 1.1 haad
56 1.1 haad MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
57 1.1 haad
58 1.1 haad static int
59 1.1 haad dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
60 1.1 haad {
61 1.1 haad dm_target_t *dmt;
62 1.1 haad int r;
63 1.1 haad dmt = NULL;
64 1.1 haad
65 1.1 haad switch (cmd) {
66 1.1 haad case MODULE_CMD_INIT:
67 1.2.6.1 jym if ((dmt = dm_target_lookup("stripe")) != NULL) {
68 1.2.6.1 jym dm_target_unbusy(dmt);
69 1.1 haad return EEXIST;
70 1.2.6.1 jym }
71 1.2.6.1 jym
72 1.1 haad dmt = dm_target_alloc("stripe");
73 1.1 haad
74 1.1 haad dmt->version[0] = 1;
75 1.1 haad dmt->version[1] = 0;
76 1.1 haad dmt->version[2] = 0;
77 1.1 haad strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
78 1.1 haad dmt->init = &dm_target_stripe_init;
79 1.1 haad dmt->status = &dm_target_stripe_status;
80 1.1 haad dmt->strategy = &dm_target_stripe_strategy;
81 1.1 haad dmt->deps = &dm_target_stripe_deps;
82 1.1 haad dmt->destroy = &dm_target_stripe_destroy;
83 1.1 haad dmt->upcall = &dm_target_stripe_upcall;
84 1.1 haad
85 1.1 haad r = dm_target_insert(dmt);
86 1.1 haad
87 1.1 haad break;
88 1.1 haad
89 1.1 haad case MODULE_CMD_FINI:
90 1.1 haad r = dm_target_rem("stripe");
91 1.1 haad break;
92 1.1 haad
93 1.1 haad case MODULE_CMD_STAT:
94 1.1 haad return ENOTTY;
95 1.1 haad
96 1.1 haad default:
97 1.1 haad return ENOTTY;
98 1.1 haad }
99 1.1 haad
100 1.1 haad return r;
101 1.1 haad }
102 1.1 haad
103 1.1 haad #endif
104 1.1 haad
105 1.2.6.1 jym /*
106 1.2.6.1 jym * Init function called from dm_table_load_ioctl.
107 1.2.6.1 jym * Example line sent to dm from lvm tools when using striped target.
108 1.2.6.1 jym * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
109 1.2.6.1 jym * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
110 1.2.6.1 jym */
111 1.1 haad int
112 1.2.6.2 jym dm_target_stripe_init(dm_dev_t *dmv, void **target_config, prop_dictionary_t dict)
113 1.1 haad {
114 1.2.6.1 jym dm_target_stripe_config_t *tsc;
115 1.2.6.2 jym prop_array_t dev_array;
116 1.2.6.2 jym prop_dictionary_t dev_dict1, dev_dict2;
117 1.2.6.2 jym
118 1.2.6.2 jym uint64_t stripes, chunk_size, offset1, offset2;
119 1.2.6.2 jym const char *device1, *device2;
120 1.2.6.2 jym
121 1.2.6.2 jym if (prop_dictionary_get_uint64(dict, DM_TARGET_STRIPE_STRIPES,
122 1.2.6.2 jym &stripes) == false)
123 1.2.6.1 jym return EINVAL;
124 1.2.6.1 jym
125 1.2.6.2 jym if (prop_dictionary_get_uint64(dict, DM_TARGET_STRIPE_CHUNKSIZE,
126 1.2.6.2 jym &chunk_size) == false)
127 1.2.6.2 jym return EINVAL;
128 1.2.6.2 jym
129 1.2.6.2 jym dev_array = prop_dictionary_get(dict, DM_TARGET_STRIPE_DEVARRAY);
130 1.2.6.1 jym
131 1.2.6.2 jym /* XXX Support for more than 2 devices */
132 1.2.6.2 jym dev_dict1 = prop_array_get(dev_array, 0);
133 1.2.6.2 jym dev_dict2 = prop_array_get(dev_array, 1);
134 1.1 haad
135 1.2.6.2 jym if(dev_dict2 == NULL || dev_dict1 == NULL)
136 1.2.6.2 jym return EINVAL;
137 1.2.6.2 jym
138 1.2.6.2 jym prop_dictionary_get_cstring_nocopy(dev_dict1, DM_TARGET_STRIPE_DEVICE, &device1);
139 1.2.6.2 jym prop_dictionary_get_uint64(dev_dict1, DM_TARGET_STRIPE_OFFSET, &offset1);
140 1.2.6.2 jym prop_dictionary_get_cstring_nocopy(dev_dict2, DM_TARGET_STRIPE_DEVICE, &device2);
141 1.2.6.2 jym prop_dictionary_get_uint64(dev_dict2, DM_TARGET_STRIPE_OFFSET, &offset2);
142 1.2.6.2 jym
143 1.2.6.2 jym aprint_debug("Stripe target init function called!!\n");
144 1.2.6.2 jym
145 1.2.6.2 jym aprint_debug("Stripe target chunk size %"PRIu64" number of stripes %"PRIu64"\n", chunk_size, stripes);
146 1.2.6.2 jym aprint_debug("Stripe target device name %s -- offset %"PRIu64"\n", device1, offset1);
147 1.2.6.2 jym aprint_debug("Stripe target device name %s -- offset %"PRIu64"\n", device2, offset2);
148 1.2.6.1 jym
149 1.2.6.2 jym if (stripes > MAX_STRIPES)
150 1.2.6.1 jym return ENOTSUP;
151 1.2.6.1 jym
152 1.2.6.1 jym if ((tsc = kmem_alloc(sizeof(dm_target_stripe_config_t), KM_NOSLEEP))
153 1.2.6.1 jym == NULL)
154 1.2.6.1 jym return ENOMEM;
155 1.2.6.1 jym
156 1.2.6.1 jym /* Insert dmp to global pdev list */
157 1.2.6.2 jym if ((tsc->stripe_devs[0].pdev = dm_pdev_insert(device1)) == NULL)
158 1.2.6.1 jym return ENOENT;
159 1.2.6.1 jym
160 1.2.6.1 jym /* Insert dmp to global pdev list */
161 1.2.6.2 jym if ((tsc->stripe_devs[1].pdev = dm_pdev_insert(device2)) == NULL)
162 1.2.6.1 jym return ENOENT;
163 1.2.6.1 jym
164 1.2.6.2 jym tsc->stripe_devs[0].offset = offset1;
165 1.2.6.2 jym tsc->stripe_devs[1].offset = offset2;
166 1.2.6.1 jym
167 1.2.6.1 jym /* Save length of param string */
168 1.2.6.2 jym tsc->params_len = DM_MAX_PARAMS_SIZE;
169 1.2.6.2 jym tsc->stripe_chunksize = chunk_size;
170 1.2.6.2 jym tsc->stripe_num = (uint8_t)stripes;
171 1.2.6.1 jym
172 1.2.6.1 jym *target_config = tsc;
173 1.1 haad
174 1.1 haad dmv->dev_type = DM_STRIPE_DEV;
175 1.1 haad
176 1.2.6.1 jym return 0;
177 1.1 haad }
178 1.1 haad
179 1.1 haad /* Status routine called to get params string. */
180 1.1 haad char *
181 1.1 haad dm_target_stripe_status(void *target_config)
182 1.1 haad {
183 1.2.6.1 jym dm_target_stripe_config_t *tsc;
184 1.2.6.1 jym char *params;
185 1.2.6.1 jym
186 1.2.6.1 jym tsc = target_config;
187 1.2.6.1 jym
188 1.2.6.1 jym if ((params = kmem_alloc(tsc->params_len, KM_NOSLEEP)) == NULL)
189 1.2.6.1 jym return NULL;
190 1.2.6.1 jym
191 1.2.6.1 jym snprintf(params, tsc->params_len, "%d %"PRIu64" %s %"PRIu64" %s %"PRIu64,
192 1.2.6.1 jym tsc->stripe_num, tsc->stripe_chunksize,
193 1.2.6.1 jym tsc->stripe_devs[0].pdev->name, tsc->stripe_devs[0].offset,
194 1.2.6.1 jym tsc->stripe_devs[1].pdev->name, tsc->stripe_devs[1].offset);
195 1.2.6.1 jym
196 1.2.6.1 jym return params;
197 1.1 haad }
198 1.1 haad
199 1.1 haad /* Strategy routine called from dm_strategy. */
200 1.1 haad int
201 1.1 haad dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
202 1.1 haad {
203 1.2.6.1 jym dm_target_stripe_config_t *tsc;
204 1.2.6.1 jym struct buf *nestbuf;
205 1.2.6.1 jym uint64_t blkno, blkoff;
206 1.2.6.1 jym uint64_t stripe, stripe_blknr;
207 1.2.6.1 jym uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
208 1.2.6.1 jym int stripe_devnr;
209 1.2.6.1 jym
210 1.2.6.1 jym tsc = table_en->target_config;
211 1.2.6.1 jym if (tsc == NULL)
212 1.2.6.1 jym return 0;
213 1.2.6.1 jym
214 1.2.6.2 jym /* aprint_debug("Stripe target read function called %" PRIu64 "!!\n",
215 1.2.6.1 jym tlc->offset);*/
216 1.2.6.1 jym
217 1.2.6.1 jym /* calculate extent of request */
218 1.2.6.1 jym KASSERT(bp->b_resid % DEV_BSIZE == 0);
219 1.2.6.1 jym
220 1.2.6.1 jym blkno = bp->b_blkno;
221 1.2.6.1 jym blkoff = 0;
222 1.2.6.1 jym num_blks = bp->b_resid / DEV_BSIZE;
223 1.2.6.1 jym for (;;) {
224 1.2.6.1 jym /* blockno to strip piece nr */
225 1.2.6.1 jym stripe = blkno / tsc->stripe_chunksize;
226 1.2.6.1 jym stripe_off = blkno % tsc->stripe_chunksize;
227 1.2.6.1 jym
228 1.2.6.1 jym /* where we are inside the strip */
229 1.2.6.1 jym stripe_devnr = stripe % tsc->stripe_num;
230 1.2.6.1 jym stripe_blknr = stripe / tsc->stripe_num;
231 1.2.6.1 jym
232 1.2.6.1 jym /* how much is left before we hit a boundary */
233 1.2.6.1 jym stripe_rest = tsc->stripe_chunksize - stripe_off;
234 1.2.6.1 jym
235 1.2.6.1 jym /* issue this piece on stripe `stripe' */
236 1.2.6.1 jym issue_blks = MIN(stripe_rest, num_blks);
237 1.2.6.1 jym nestbuf = getiobuf(NULL, true);
238 1.2.6.1 jym
239 1.2.6.1 jym nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
240 1.2.6.1 jym nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
241 1.2.6.1 jym nestbuf->b_blkno += tsc->stripe_devs[stripe_devnr].offset;
242 1.2.6.1 jym
243 1.2.6.1 jym VOP_STRATEGY(tsc->stripe_devs[stripe_devnr].pdev->pdev_vnode, nestbuf);
244 1.2.6.1 jym
245 1.2.6.1 jym blkno += issue_blks;
246 1.2.6.1 jym blkoff += issue_blks * DEV_BSIZE;
247 1.2.6.1 jym num_blks -= issue_blks;
248 1.1 haad
249 1.2.6.1 jym if (num_blks <= 0)
250 1.2.6.1 jym break;
251 1.2.6.1 jym }
252 1.1 haad
253 1.1 haad return 0;
254 1.1 haad }
255 1.1 haad
256 1.1 haad /* Doesn't do anything here. */
257 1.1 haad int
258 1.1 haad dm_target_stripe_destroy(dm_table_entry_t *table_en)
259 1.1 haad {
260 1.2.6.1 jym dm_target_stripe_config_t *tsc;
261 1.2.6.1 jym
262 1.2.6.1 jym tsc = table_en->target_config;
263 1.2.6.1 jym
264 1.2.6.1 jym if (tsc == NULL)
265 1.2.6.1 jym return 0;
266 1.2.6.1 jym
267 1.2.6.1 jym dm_pdev_decr(tsc->stripe_devs[0].pdev);
268 1.2.6.1 jym dm_pdev_decr(tsc->stripe_devs[1].pdev);
269 1.1 haad
270 1.1 haad /* Unbusy target so we can unload it */
271 1.1 haad dm_target_unbusy(table_en->target);
272 1.1 haad
273 1.2.6.1 jym kmem_free(tsc, sizeof(dm_target_stripe_config_t));
274 1.2.6.1 jym
275 1.2.6.1 jym table_en->target_config = NULL;
276 1.2.6.1 jym
277 1.1 haad return 0;
278 1.1 haad }
279 1.1 haad
280 1.1 haad /* Doesn't not need to do anything here. */
281 1.1 haad int
282 1.1 haad dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
283 1.1 haad {
284 1.2.6.1 jym dm_target_stripe_config_t *tsc;
285 1.2.6.1 jym struct vattr va;
286 1.2.6.1 jym
287 1.2.6.1 jym int error;
288 1.2.6.1 jym
289 1.2.6.1 jym if (table_en->target_config == NULL)
290 1.2.6.1 jym return ENOENT;
291 1.2.6.1 jym
292 1.2.6.1 jym tsc = table_en->target_config;
293 1.2.6.1 jym
294 1.2.6.1 jym if ((error = VOP_GETATTR(tsc->stripe_devs[0].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
295 1.2.6.1 jym return error;
296 1.2.6.1 jym
297 1.2.6.1 jym prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
298 1.2.6.1 jym
299 1.2.6.1 jym if ((error = VOP_GETATTR(tsc->stripe_devs[1].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
300 1.2.6.1 jym return error;
301 1.2.6.1 jym
302 1.2.6.1 jym prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
303 1.2.6.1 jym
304 1.1 haad return 0;
305 1.1 haad }
306 1.1 haad
307 1.1 haad /* Unsupported for this target. */
308 1.1 haad int
309 1.1 haad dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
310 1.1 haad {
311 1.1 haad return 0;
312 1.1 haad }
313