Merge branch 'vendor/OPENSSL'
[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, blknr;
236         uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
237         int 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         switch(bp->b_cmd) {
247         case BUF_CMD_READ:
248         case BUF_CMD_WRITE:
249         case BUF_CMD_FREEBLKS:
250                 /*
251                  * Loop through to individual operations
252                  */
253                 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
254                 blkoff = 0;
255                 num_blks = bp->b_resid / DEV_BSIZE;
256                 nestiobuf_init(bio);
257
258                 while (num_blks > 0) {
259                         /* blockno to strip piece nr */
260                         stripe = blkno / tsc->stripe_chunksize;
261                         stripe_off = blkno % tsc->stripe_chunksize;
262
263                         /* where we are inside the strip */
264                         devnr = stripe % tsc->stripe_num;
265                         blknr = stripe / tsc->stripe_num;
266
267                         /* how much is left before we hit a boundary */
268                         stripe_rest = tsc->stripe_chunksize - stripe_off;
269
270                         /* issue this piece on stripe `stripe' */
271                         issue_blks = MIN(stripe_rest, num_blks);
272                         nestbuf = getpbuf(NULL);
273                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
274
275                         nestiobuf_add(bio, nestbuf, blkoff,
276                                         issue_blks * DEV_BSIZE);
277
278                         /* I need number of bytes. */
279                         nestbuf->b_bio1.bio_offset =
280                                 blknr * tsc->stripe_chunksize + stripe_off;
281                         nestbuf->b_bio1.bio_offset +=
282                                 tsc->stripe_devs[devnr].offset;
283                         nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
284
285                         vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
286                                     &nestbuf->b_bio1);
287
288                         blkno += issue_blks;
289                         blkoff += issue_blks * DEV_BSIZE;
290                         num_blks -= issue_blks;
291                 }
292                 nestiobuf_start(bio);
293                 break;
294         case BUF_CMD_FLUSH:
295                 nestiobuf_init(bio);
296                 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
297                         nestbuf = getpbuf(NULL);
298                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
299
300                         nestiobuf_add(bio, nestbuf, 0, 0);
301                         nestbuf->b_bio1.bio_offset = 0;
302                         vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
303                                     &nestbuf->b_bio1);
304                 }
305                 nestiobuf_start(bio);
306                 break;
307         default:
308                 bp->b_flags |= B_ERROR;
309                 bp->b_error = EIO;
310                 biodone(bio);
311                 break;
312         }
313         return 0;
314 }
315
316 /*
317  * Destroy a dm table entry for stripes.
318  */
319 int
320 dm_target_stripe_destroy(dm_table_entry_t *table_en)
321 {
322         dm_target_stripe_config_t *tsc;
323
324         if ((tsc = table_en->target_config) != NULL) {
325                 table_en->target_config = NULL;
326                 dm_target_stripe_destroy_config(tsc);
327         }
328
329         /* Unbusy target so we can unload it */
330         dm_target_unbusy(table_en->target);
331
332         return 0;
333 }
334
335 static void
336 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
337 {
338         int n;
339
340         for (n = 0; n < tsc->stripe_num; ++n) {
341                 if (tsc->stripe_devs[n].pdev) {
342                         dm_pdev_decr(tsc->stripe_devs[n].pdev);
343                         tsc->stripe_devs[n].pdev = NULL;
344                 }
345         }
346         kfree(tsc, M_DMSTRIPE);
347 }
348
349 /*
350  * Generate properties from stripe table entry.
351  */
352 int
353 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
354 {
355         dm_target_stripe_config_t *tsc;
356         struct vattr va;
357         int error;
358         int n;
359
360         if (table_en->target_config == NULL)
361                 return ENOENT;
362
363         tsc = table_en->target_config;
364         error = 0;
365         for (n = 0; n < tsc->stripe_num; ++n) {
366                 error = VOP_GETATTR(tsc->stripe_devs[n].pdev->pdev_vnode, &va);
367                 if (error)
368                         break;
369                 prop_array_add_uint64(prop_array,
370                                 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
371         }
372         return (error);
373 }
374
375 /*
376  * Unsupported for this target.
377  */
378 int
379 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)
380 {
381         return 0;
382 }