kernel - dm - rewrite dm_target_stripe.c to add functionality and fix bugs
[dragonfly.git] / sys / dev / disk / dm / dm_target_stripe.c
1 /*
2  * Copyright (c) 2009 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Adam Hamsik.
7  *
8  * This code is further derived from software contributed to the
9  * DragonFly project by Alex Hornung and Matthew Dillon
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $NetBSD: dm_target_stripe.c,v 1.9 2010/01/04 00:14:41 haad Exp $
33  */
34
35 /*
36  * This file implements initial version of device-mapper stripe target.
37  *
38  * DragonFly changes: Increase to an unlimited number of stripes
39  */
40 #include <sys/types.h>
41 #include <sys/param.h>
42
43 #include <sys/buf.h>
44 #include <sys/malloc.h>
45 #include <sys/vnode.h>
46
47 #include "dm.h"
48 MALLOC_DEFINE(M_DMSTRIPE, "dm_stripe", "Device Mapper Target Stripe");
49
50 static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc);
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 static int
64 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
65 {
66         dm_target_t *dmt;
67         int r;
68         dmt = NULL;
69
70         switch (cmd) {
71         case MODULE_CMD_INIT:
72                 if ((dmt = dm_target_lookup("stripe")) != NULL) {
73                         dm_target_unbusy(dmt);
74                         return EEXIST;
75                 }
76                 dmt = dm_target_alloc("stripe");
77
78                 dmt->version[0] = 1;
79                 dmt->version[1] = 0;
80                 dmt->version[2] = 0;
81                 strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
82                 dmt->init = &dm_target_stripe_init;
83                 dmt->status = &dm_target_stripe_status;
84                 dmt->strategy = &dm_target_stripe_strategy;
85                 dmt->deps = &dm_target_stripe_deps;
86                 dmt->destroy = &dm_target_stripe_destroy;
87                 dmt->upcall = &dm_target_stripe_upcall;
88
89                 r = dm_target_insert(dmt);
90
91                 break;
92
93         case MODULE_CMD_FINI:
94                 r = dm_target_rem("stripe");
95                 break;
96
97         case MODULE_CMD_STAT:
98                 return ENOTTY;
99
100         default:
101                 return ENOTTY;
102         }
103
104         return r;
105 }
106 #endif
107
108 /*
109  * Init function called from dm_table_load_ioctl.
110  *
111  * Example line sent to dm from lvm tools when using striped target.
112  * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
113  * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
114  */
115 int
116 dm_target_stripe_init(dm_dev_t *dmv, void **target_config, char *params)
117 {
118         dm_target_stripe_config_t *tsc;
119         int n;
120         char *ap;
121
122         if (params == NULL)
123                 return EINVAL;
124
125         /*
126          * nstripes
127          */
128         ap = strsep(&params, " \t");
129         if (ap == NULL)
130                 return EINVAL;
131         n = (int)atoi64(ap);
132         if (n < 0 || n > MAX_STRIPES) {
133                 kprintf("dm: Error %d stripes not supported (%d max)\n",
134                         n, MAX_STRIPES);
135                 return ENOTSUP;
136         }
137
138         tsc = kmalloc(sizeof(dm_target_stripe_config_t),
139                       M_DMSTRIPE, M_WAITOK | M_ZERO);
140         tsc->stripe_num = n;
141
142         ap = strsep(&params, " \t");
143         if (ap == NULL)
144                 return EINVAL;
145         tsc->stripe_chunksize = atoi64(ap);
146         if (tsc->stripe_chunksize < 1 ||
147             tsc->stripe_chunksize * DEV_BSIZE > MAXPHYS) {
148                 kprintf("dm: Error unsupported chunk size %jdKB\n",
149                         (intmax_t)tsc->stripe_chunksize * DEV_BSIZE / 1024);
150                 dm_target_stripe_destroy_config(tsc);
151                 return EINVAL;
152         }
153
154         /*
155          * Parse the devices
156          */
157
158         kprintf("dm: Stripe %d devices chunk size %dKB\n",
159                 (int)tsc->stripe_num,
160                 (int)tsc->stripe_chunksize
161         );
162
163         for (n = 0; n < tsc->stripe_num; ++n) {
164                 ap = strsep(&params, " \t");
165                 if (ap == NULL)
166                         break;
167                 tsc->stripe_devs[n].pdev = dm_pdev_insert(ap);
168                 if (tsc->stripe_devs[n].pdev == NULL)
169                         break;
170                 ap = strsep(&params, " \t");
171                 if (ap == NULL)
172                         break;
173                 tsc->stripe_devs[n].offset = atoi64(ap);
174         }
175         if (n != tsc->stripe_num) {
176                 dm_target_stripe_destroy_config(tsc);
177                 return (ENOENT);
178         }
179
180         *target_config = tsc;
181
182         dmv->dev_type = DM_STRIPE_DEV;
183
184         return 0;
185 }
186
187 /*
188  * Status routine called to get params string.
189  */
190 char *
191 dm_target_stripe_status(void *target_config)
192 {
193         dm_target_stripe_config_t *tsc;
194         char *params;
195         char *ptr;
196         size_t len;
197         size_t nlen;
198         int n;
199
200         tsc = target_config;
201
202         /* caller expects use of M_DM for returned params */
203         nlen = DM_MAX_PARAMS_SIZE;
204         params = kmalloc(nlen, M_DM, M_WAITOK);
205         ptr = params;
206
207         ksnprintf(ptr, nlen, "%d %jd",
208                   tsc->stripe_num, (intmax_t)tsc->stripe_chunksize);
209         len = strlen(params);
210         ptr += len;
211         nlen -= len;
212
213         for (n = 0; n < tsc->stripe_num; ++n) {
214                 ksnprintf(ptr, nlen, " %s %jd",
215                           tsc->stripe_devs[n].pdev->name,
216                           (intmax_t)tsc->stripe_devs[n].offset);
217                 len = strlen(ptr);
218                 ptr += len;
219                 nlen -= len;
220         }
221
222         return params;
223 }
224
225 /*
226  * Strategy routine called from dm_strategy.
227  */
228 int
229 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
230 {
231         dm_target_stripe_config_t *tsc;
232         struct bio *bio = &bp->b_bio1;
233         struct buf *nestbuf;
234         uint64_t blkno, blkoff;
235         uint64_t stripe, stripe_blknr;
236         uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
237         int stripe_devnr;
238
239         tsc = table_en->target_config;
240         if (tsc == NULL)
241                 return 0;
242
243         /* calculate extent of request */
244         KKASSERT(bp->b_resid % DEV_BSIZE == 0);
245
246         blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
247         blkoff = 0;
248         num_blks = bp->b_resid / DEV_BSIZE;
249
250         for (;;) {
251                 /* blockno to strip piece nr */
252                 stripe = blkno / tsc->stripe_chunksize;
253                 stripe_off = blkno % tsc->stripe_chunksize;
254
255                 /* where we are inside the strip */
256                 stripe_devnr = stripe % tsc->stripe_num;
257                 stripe_blknr = stripe / tsc->stripe_num;
258
259                 /* how much is left before we hit a boundary */
260                 stripe_rest = tsc->stripe_chunksize - stripe_off;
261
262                 /* issue this piece on stripe `stripe' */
263                 issue_blks = MIN(stripe_rest, num_blks);
264                 nestbuf = getpbuf(NULL);
265
266                 nestiobuf_setup(bio, nestbuf, blkoff, issue_blks * DEV_BSIZE);
267
268                 /* I need number of bytes. */
269                 nestbuf->b_bio1.bio_offset =
270                         stripe_blknr * tsc->stripe_chunksize + stripe_off;
271                 nestbuf->b_bio1.bio_offset +=
272                         tsc->stripe_devs[stripe_devnr].offset;
273                 nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
274
275                 vn_strategy(tsc->stripe_devs[stripe_devnr].pdev->pdev_vnode,
276                             &nestbuf->b_bio1);
277
278                 blkno += issue_blks;
279                 blkoff += issue_blks * DEV_BSIZE;
280                 num_blks -= issue_blks;
281
282                 if (num_blks <= 0)
283                         break;
284         }
285
286         return 0;
287 }
288
289 /*
290  * Destroy a dm table entry for stripes.
291  */
292 int
293 dm_target_stripe_destroy(dm_table_entry_t *table_en)
294 {
295         dm_target_stripe_config_t *tsc;
296
297         if ((tsc = table_en->target_config) != NULL) {
298                 table_en->target_config = NULL;
299                 dm_target_stripe_destroy_config(tsc);
300         }
301
302         /* Unbusy target so we can unload it */
303         dm_target_unbusy(table_en->target);
304
305         return 0;
306 }
307
308 static void
309 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
310 {
311         int n;
312
313         for (n = 0; n < tsc->stripe_num; ++n) {
314                 if (tsc->stripe_devs[n].pdev) {
315                         dm_pdev_decr(tsc->stripe_devs[n].pdev);
316                         tsc->stripe_devs[n].pdev = NULL;
317                 }
318         }
319         kfree(tsc, M_DMSTRIPE);
320 }
321
322 /*
323  * Generate properties from stripe table entry.
324  */
325 int
326 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
327 {
328         dm_target_stripe_config_t *tsc;
329         struct vattr va;
330         int error;
331         int n;
332
333         if (table_en->target_config == NULL)
334                 return ENOENT;
335
336         tsc = table_en->target_config;
337         error = 0;
338         for (n = 0; n < tsc->stripe_num; ++n) {
339                 error = VOP_GETATTR(tsc->stripe_devs[n].pdev->pdev_vnode, &va);
340                 if (error)
341                         break;
342                 prop_array_add_uint64(prop_array,
343                                 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
344         }
345         return (error);
346 }
347
348 /*
349  * Unsupported for this target.
350  */
351 int
352 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)
353 {
354         return 0;
355 }