sys/dev/disk/dm: Use correct module name for dm_target_striped
[dragonfly.git] / sys / dev / disk / dm / targets / striped / dm_target_striped.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
42 #include <sys/buf.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
45
46 #include <dev/disk/dm/dm.h>
47 MALLOC_DEFINE(M_DMSTRIPE, "dm_striped", "Device Mapper Target Striped");
48
49 #define MAX_STRIPES 32
50
51 struct target_stripe_dev {
52         dm_pdev_t *pdev;
53         uint64_t offset;
54 };
55
56 typedef struct target_stripe_config {
57         struct target_stripe_dev stripe_devs[MAX_STRIPES];
58         int stripe_num;
59         uint64_t stripe_chunksize;
60 } dm_target_stripe_config_t;
61
62 static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc);
63
64 /*
65  * Init function called from dm_table_load_ioctl.
66  *
67  * Example line sent to dm from lvm tools when using striped target.
68  * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
69  * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
70  */
71 static int
72 dm_target_stripe_init(dm_table_entry_t *table_en, char *params)
73 {
74         dm_target_stripe_config_t *tsc;
75         int n;
76         char *ap;
77
78         if (params == NULL)
79                 return EINVAL;
80
81         /*
82          * nstripes
83          */
84         ap = strsep(&params, " \t");
85         if (ap == NULL)
86                 return EINVAL;
87         n = (int)atoi64(ap);
88         if (n < 0 || n > MAX_STRIPES) {
89                 kprintf("dm: Error %d stripes not supported (%d max)\n",
90                         n, MAX_STRIPES);
91                 return ENOTSUP;
92         }
93
94         tsc = kmalloc(sizeof(dm_target_stripe_config_t),
95                       M_DMSTRIPE, M_WAITOK | M_ZERO);
96         tsc->stripe_num = n;
97
98         ap = strsep(&params, " \t");
99         if (ap == NULL) {
100                 dm_target_stripe_destroy_config(tsc);
101                 return EINVAL;
102         }
103         tsc->stripe_chunksize = atoi64(ap);
104         if (tsc->stripe_chunksize < 1 ||
105             tsc->stripe_chunksize * DEV_BSIZE > MAXPHYS) {
106                 kprintf("dm: Error unsupported chunk size %jdKB\n",
107                         (intmax_t)tsc->stripe_chunksize * DEV_BSIZE / 1024);
108                 dm_target_stripe_destroy_config(tsc);
109                 return EINVAL;
110         }
111
112         /*
113          * Parse the devices
114          */
115
116         kprintf("dm: Stripe %d devices chunk size %dKB\n",
117                 (int)tsc->stripe_num,
118                 (int)tsc->stripe_chunksize
119         );
120
121         for (n = 0; n < tsc->stripe_num; ++n) {
122                 ap = strsep(&params, " \t");
123                 if (ap == NULL)
124                         break;
125                 tsc->stripe_devs[n].pdev = dm_pdev_insert(ap);
126                 if (tsc->stripe_devs[n].pdev == NULL)
127                         break;
128                 ap = strsep(&params, " \t");
129                 if (ap == NULL)
130                         break;
131                 tsc->stripe_devs[n].offset = atoi64(ap);
132                 dm_table_add_deps(table_en, tsc->stripe_devs[n].pdev);
133         }
134         if (n != tsc->stripe_num) {
135                 dm_target_stripe_destroy_config(tsc);
136                 return (ENOENT);
137         }
138
139         dm_table_init_target(table_en, DM_STRIPE_DEV, tsc);
140
141         return 0;
142 }
143
144 /*
145  * Info routine called to get params string.
146  */
147 static char *
148 dm_target_stripe_info(void *target_config)
149 {
150         dm_target_stripe_config_t *tsc;
151         char *params;
152         char *ptr;
153         size_t len;
154         int ret;
155         int i;
156
157         tsc = target_config;
158
159         len = DM_MAX_PARAMS_SIZE;
160         params = kmalloc(len, M_DM, M_WAITOK | M_ZERO);
161         ptr = params;
162
163         ret = ksnprintf(ptr, len, "%d ", tsc->stripe_num);
164         ptr += ret;
165         len -= ret;
166
167         for (i = 0; i < tsc->stripe_num; i++) {
168                 ret = ksnprintf(ptr, len, "%s ",
169                         tsc->stripe_devs[i].pdev->udev_name);
170                 ptr += ret;
171                 len -= ret;
172         }
173
174         ret = ksnprintf(ptr, len, "1 ");
175         ptr += ret;
176         len -= ret;
177
178         /*
179          * stripe target currently has no error_count.
180          */
181         for (i = 0; i < tsc->stripe_num; i++) {
182                 ret = ksnprintf(ptr, len, "A");
183                 ptr += ret;
184                 len -= ret;
185         }
186
187         return params;
188 }
189
190 /*
191  * Table routine called to get params string.
192  */
193 static char *
194 dm_target_stripe_table(void *target_config)
195 {
196         dm_target_stripe_config_t *tsc;
197         char *params;
198         char *ptr;
199         size_t len;
200         int ret;
201         int i;
202
203         tsc = target_config;
204
205         len = DM_MAX_PARAMS_SIZE;
206         params = kmalloc(len, M_DM, M_WAITOK | M_ZERO);
207         ptr = params;
208
209         ret = ksnprintf(ptr, len, "%d %jd",
210                 tsc->stripe_num,
211                 (intmax_t)tsc->stripe_chunksize);
212         ptr += ret;
213         len -= ret;
214
215         for (i = 0; i < tsc->stripe_num; i++) {
216                 ret = ksnprintf(ptr, len, " %s %jd",
217                         tsc->stripe_devs[i].pdev->udev_name,
218                         (intmax_t)tsc->stripe_devs[i].offset);
219                 ptr += ret;
220                 len -= ret;
221         }
222
223         return params;
224 }
225
226 /*
227  * Strategy routine called from dm_strategy.
228  */
229 static int
230 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
231 {
232         dm_target_stripe_config_t *tsc;
233         struct bio *bio = &bp->b_bio1;
234         struct buf *nestbuf;
235         uint64_t blkno, blkoff;
236         uint64_t stripe, blknr;
237         uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
238         int devnr;
239
240         tsc = table_en->target_config;
241         if (tsc == NULL)
242                 return 0;
243
244         /* calculate extent of request */
245         KKASSERT(bp->b_resid % DEV_BSIZE == 0);
246
247         switch(bp->b_cmd) {
248         case BUF_CMD_READ:
249         case BUF_CMD_WRITE:
250         case BUF_CMD_FREEBLKS:
251                 /*
252                  * Loop through to individual operations
253                  */
254                 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
255                 blkoff = 0;
256                 num_blks = bp->b_resid / DEV_BSIZE;
257                 nestiobuf_init(bio);
258
259                 while (num_blks > 0) {
260                         /* blockno to strip piece nr */
261                         stripe = blkno / tsc->stripe_chunksize;
262                         stripe_off = blkno % tsc->stripe_chunksize;
263
264                         /* where we are inside the strip */
265                         devnr = stripe % tsc->stripe_num;
266                         blknr = stripe / tsc->stripe_num;
267
268                         /* how much is left before we hit a boundary */
269                         stripe_rest = tsc->stripe_chunksize - stripe_off;
270
271                         /* issue this piece on stripe `stripe' */
272                         issue_blks = MIN(stripe_rest, num_blks);
273                         nestbuf = getpbuf(NULL);
274                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
275
276                         nestiobuf_add(bio, nestbuf, blkoff,
277                                         issue_blks * DEV_BSIZE, NULL);
278
279                         /* I need number of bytes. */
280                         nestbuf->b_bio1.bio_offset =
281                                 blknr * tsc->stripe_chunksize + stripe_off;
282                         nestbuf->b_bio1.bio_offset +=
283                                 tsc->stripe_devs[devnr].offset;
284                         nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
285
286                         vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
287                                     &nestbuf->b_bio1);
288
289                         blkno += issue_blks;
290                         blkoff += issue_blks * DEV_BSIZE;
291                         num_blks -= issue_blks;
292                 }
293                 nestiobuf_start(bio);
294                 break;
295         case BUF_CMD_FLUSH:
296                 nestiobuf_init(bio);
297                 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
298                         nestbuf = getpbuf(NULL);
299                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
300
301                         nestiobuf_add(bio, nestbuf, 0, 0, NULL);
302                         nestbuf->b_bio1.bio_offset = 0;
303                         vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
304                                     &nestbuf->b_bio1);
305                 }
306                 nestiobuf_start(bio);
307                 break;
308         default:
309                 bp->b_flags |= B_ERROR;
310                 bp->b_error = EIO;
311                 biodone(bio);
312                 break;
313         }
314         return 0;
315 }
316
317
318 static int
319 dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
320 {
321         dm_target_stripe_config_t *tsc;
322         uint64_t blkno, blkoff;
323         uint64_t stripe, blknr;
324         uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
325         uint64_t off2, len2;
326         int devnr;
327
328         tsc = table_en->target_config;
329         if (tsc == NULL)
330                 return 0;
331
332         /* calculate extent of request */
333         KKASSERT(length % DEV_BSIZE == 0);
334
335         blkno = offset / DEV_BSIZE;
336         blkoff = 0;
337         num_blks = length / DEV_BSIZE;
338
339         /*
340          * 0 length means flush buffers and return
341          */
342         if (length == 0) {
343                 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
344                         if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
345                                 return ENXIO;
346
347                         dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
348                             data, 0, offset, 0);
349                 }
350                 return 0;
351         }
352
353         while (num_blks > 0) {
354                 /* blockno to strip piece nr */
355                 stripe = blkno / tsc->stripe_chunksize;
356                 stripe_off = blkno % tsc->stripe_chunksize;
357
358                 /* where we are inside the strip */
359                 devnr = stripe % tsc->stripe_num;
360                 blknr = stripe / tsc->stripe_num;
361
362                 /* how much is left before we hit a boundary */
363                 stripe_rest = tsc->stripe_chunksize - stripe_off;
364
365                 /* issue this piece on stripe `stripe' */
366                 issue_blks = MIN(stripe_rest, num_blks);
367
368 #if 0
369                 nestiobuf_add(bio, nestbuf, blkoff,
370                                 issue_blks * DEV_BSIZE);
371 #endif
372                 len2 = issue_blks * DEV_BSIZE;
373
374                 /* I need number of bytes. */
375                 off2 = blknr * tsc->stripe_chunksize + stripe_off;
376                 off2 += tsc->stripe_devs[devnr].offset;
377                 off2 *= DEV_BSIZE;
378                 off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev,
379                     off2);
380
381                 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
382                         return ENXIO;
383
384                 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
385                     (char *)data + blkoff, 0, off2, len2);
386
387                 blkno += issue_blks;
388                 blkoff += issue_blks * DEV_BSIZE;
389                 num_blks -= issue_blks;
390         }
391
392         return 0;
393 }
394
395 /*
396  * Destroy a dm table entry for stripes.
397  */
398 static int
399 dm_target_stripe_destroy(dm_table_entry_t *table_en)
400 {
401         dm_target_stripe_config_t *tsc;
402
403         if ((tsc = table_en->target_config) != NULL) {
404                 table_en->target_config = NULL;
405                 dm_target_stripe_destroy_config(tsc);
406         }
407
408         return 0;
409 }
410
411 static void
412 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
413 {
414         int n;
415
416         for (n = 0; n < tsc->stripe_num; ++n) {
417                 if (tsc->stripe_devs[n].pdev) {
418                         dm_pdev_decr(tsc->stripe_devs[n].pdev);
419                         tsc->stripe_devs[n].pdev = NULL;
420                 }
421         }
422         kfree(tsc, M_DMSTRIPE);
423 }
424
425 /*
426  * Unsupported for this target.
427  */
428 static int
429 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
430 {
431         return 0;
432 }
433
434 static int
435 dmts_mod_handler(module_t mod, int type, void *unused)
436 {
437         dm_target_t *dmt = NULL;
438         int err = 0;
439
440         switch(type) {
441         case MOD_LOAD:
442                 if ((dmt = dm_target_lookup("striped")) != NULL) {
443                         dm_target_unbusy(dmt);
444                         return EEXIST;
445                 }
446                 dmt = dm_target_alloc("striped");
447                 dmt->version[0] = 1;
448                 dmt->version[1] = 0;
449                 dmt->version[2] = 3;
450                 strlcpy(dmt->name, "striped", DM_MAX_TYPE_NAME);
451                 dmt->init = &dm_target_stripe_init;
452                 dmt->info = &dm_target_stripe_info;
453                 dmt->table = &dm_target_stripe_table;
454                 dmt->strategy = &dm_target_stripe_strategy;
455                 dmt->destroy = &dm_target_stripe_destroy;
456                 dmt->upcall = &dm_target_stripe_upcall;
457                 dmt->dump = &dm_target_stripe_dump;
458
459                 err = dm_target_insert(dmt);
460                 if (err == 0)
461                         kprintf("dm_target_striped: Successfully initialized\n");
462                 break;
463
464         case MOD_UNLOAD:
465                 err = dm_target_rem("striped");
466                 if (err == 0)
467                         kprintf("dm_target_striped: unloaded\n");
468                 break;
469
470         default:
471                 break;
472         }
473
474         return err;
475 }
476
477 DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler);