sys/dev/disk/dm: Add dm_table_init_target()
[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_stripe", "Device Mapper Target Stripe");
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  * Table routine called to get params string.
146  */
147 static char *
148 dm_target_stripe_table(void *target_config)
149 {
150         dm_target_stripe_config_t *tsc;
151         char *params;
152         char *ptr;
153         size_t len;
154         size_t nlen;
155         int n;
156
157         tsc = target_config;
158
159         /* caller expects use of M_DM for returned params */
160         nlen = DM_MAX_PARAMS_SIZE;
161         params = kmalloc(nlen, M_DM, M_WAITOK);
162         ptr = params;
163
164         ksnprintf(ptr, nlen, "%d %jd",
165                   tsc->stripe_num, (intmax_t)tsc->stripe_chunksize);
166         len = strlen(params);
167         ptr += len;
168         nlen -= len;
169
170         for (n = 0; n < tsc->stripe_num; ++n) {
171                 ksnprintf(ptr, nlen, " %s %jd",
172                           tsc->stripe_devs[n].pdev->name,
173                           (intmax_t)tsc->stripe_devs[n].offset);
174                 len = strlen(ptr);
175                 ptr += len;
176                 nlen -= len;
177         }
178
179         return params;
180 }
181
182 /*
183  * Strategy routine called from dm_strategy.
184  */
185 static int
186 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
187 {
188         dm_target_stripe_config_t *tsc;
189         struct bio *bio = &bp->b_bio1;
190         struct buf *nestbuf;
191         uint64_t blkno, blkoff;
192         uint64_t stripe, blknr;
193         uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
194         int devnr;
195
196         tsc = table_en->target_config;
197         if (tsc == NULL)
198                 return 0;
199
200         /* calculate extent of request */
201         KKASSERT(bp->b_resid % DEV_BSIZE == 0);
202
203         switch(bp->b_cmd) {
204         case BUF_CMD_READ:
205         case BUF_CMD_WRITE:
206         case BUF_CMD_FREEBLKS:
207                 /*
208                  * Loop through to individual operations
209                  */
210                 blkno = bp->b_bio1.bio_offset / DEV_BSIZE;
211                 blkoff = 0;
212                 num_blks = bp->b_resid / DEV_BSIZE;
213                 nestiobuf_init(bio);
214
215                 while (num_blks > 0) {
216                         /* blockno to strip piece nr */
217                         stripe = blkno / tsc->stripe_chunksize;
218                         stripe_off = blkno % tsc->stripe_chunksize;
219
220                         /* where we are inside the strip */
221                         devnr = stripe % tsc->stripe_num;
222                         blknr = stripe / tsc->stripe_num;
223
224                         /* how much is left before we hit a boundary */
225                         stripe_rest = tsc->stripe_chunksize - stripe_off;
226
227                         /* issue this piece on stripe `stripe' */
228                         issue_blks = MIN(stripe_rest, num_blks);
229                         nestbuf = getpbuf(NULL);
230                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
231
232                         nestiobuf_add(bio, nestbuf, blkoff,
233                                         issue_blks * DEV_BSIZE, NULL);
234
235                         /* I need number of bytes. */
236                         nestbuf->b_bio1.bio_offset =
237                                 blknr * tsc->stripe_chunksize + stripe_off;
238                         nestbuf->b_bio1.bio_offset +=
239                                 tsc->stripe_devs[devnr].offset;
240                         nestbuf->b_bio1.bio_offset *= DEV_BSIZE;
241
242                         vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
243                                     &nestbuf->b_bio1);
244
245                         blkno += issue_blks;
246                         blkoff += issue_blks * DEV_BSIZE;
247                         num_blks -= issue_blks;
248                 }
249                 nestiobuf_start(bio);
250                 break;
251         case BUF_CMD_FLUSH:
252                 nestiobuf_init(bio);
253                 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
254                         nestbuf = getpbuf(NULL);
255                         nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
256
257                         nestiobuf_add(bio, nestbuf, 0, 0, NULL);
258                         nestbuf->b_bio1.bio_offset = 0;
259                         vn_strategy(tsc->stripe_devs[devnr].pdev->pdev_vnode,
260                                     &nestbuf->b_bio1);
261                 }
262                 nestiobuf_start(bio);
263                 break;
264         default:
265                 bp->b_flags |= B_ERROR;
266                 bp->b_error = EIO;
267                 biodone(bio);
268                 break;
269         }
270         return 0;
271 }
272
273
274 static int
275 dm_target_stripe_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
276 {
277         dm_target_stripe_config_t *tsc;
278         uint64_t blkno, blkoff;
279         uint64_t stripe, blknr;
280         uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
281         uint64_t off2, len2;
282         int devnr;
283
284         tsc = table_en->target_config;
285         if (tsc == NULL)
286                 return 0;
287
288         /* calculate extent of request */
289         KKASSERT(length % DEV_BSIZE == 0);
290
291         blkno = offset / DEV_BSIZE;
292         blkoff = 0;
293         num_blks = length / DEV_BSIZE;
294
295         /*
296          * 0 length means flush buffers and return
297          */
298         if (length == 0) {
299                 for (devnr = 0; devnr < tsc->stripe_num; ++devnr) {
300                         if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
301                                 return ENXIO;
302
303                         dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
304                             data, 0, offset, 0);
305                 }
306                 return 0;
307         }
308
309         while (num_blks > 0) {
310                 /* blockno to strip piece nr */
311                 stripe = blkno / tsc->stripe_chunksize;
312                 stripe_off = blkno % tsc->stripe_chunksize;
313
314                 /* where we are inside the strip */
315                 devnr = stripe % tsc->stripe_num;
316                 blknr = stripe / tsc->stripe_num;
317
318                 /* how much is left before we hit a boundary */
319                 stripe_rest = tsc->stripe_chunksize - stripe_off;
320
321                 /* issue this piece on stripe `stripe' */
322                 issue_blks = MIN(stripe_rest, num_blks);
323
324 #if 0
325                 nestiobuf_add(bio, nestbuf, blkoff,
326                                 issue_blks * DEV_BSIZE);
327 #endif
328                 len2 = issue_blks * DEV_BSIZE;
329
330                 /* I need number of bytes. */
331                 off2 = blknr * tsc->stripe_chunksize + stripe_off;
332                 off2 += tsc->stripe_devs[devnr].offset;
333                 off2 *= DEV_BSIZE;
334                 off2 = dm_pdev_correct_dump_offset(tsc->stripe_devs[devnr].pdev,
335                     off2);
336
337                 if (tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev == NULL)
338                         return ENXIO;
339
340                 dev_ddump(tsc->stripe_devs[devnr].pdev->pdev_vnode->v_rdev,
341                     (char *)data + blkoff, 0, off2, len2);
342
343                 blkno += issue_blks;
344                 blkoff += issue_blks * DEV_BSIZE;
345                 num_blks -= issue_blks;
346         }
347
348         return 0;
349 }
350
351 /*
352  * Destroy a dm table entry for stripes.
353  */
354 static int
355 dm_target_stripe_destroy(dm_table_entry_t *table_en)
356 {
357         dm_target_stripe_config_t *tsc;
358
359         if ((tsc = table_en->target_config) != NULL) {
360                 table_en->target_config = NULL;
361                 dm_target_stripe_destroy_config(tsc);
362         }
363
364         return 0;
365 }
366
367 static void
368 dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc)
369 {
370         int n;
371
372         for (n = 0; n < tsc->stripe_num; ++n) {
373                 if (tsc->stripe_devs[n].pdev) {
374                         dm_pdev_decr(tsc->stripe_devs[n].pdev);
375                         tsc->stripe_devs[n].pdev = NULL;
376                 }
377         }
378         kfree(tsc, M_DMSTRIPE);
379 }
380
381 /*
382  * Unsupported for this target.
383  */
384 static int
385 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
386 {
387         return 0;
388 }
389
390 static int
391 dmts_mod_handler(module_t mod, int type, void *unused)
392 {
393         dm_target_t *dmt = NULL;
394         int err = 0;
395
396         switch(type) {
397         case MOD_LOAD:
398                 if ((dmt = dm_target_lookup("striped")) != NULL) {
399                         dm_target_unbusy(dmt);
400                         return EEXIST;
401                 }
402                 dmt = dm_target_alloc("striped");
403                 dmt->version[0] = 1;
404                 dmt->version[1] = 0;
405                 dmt->version[2] = 3;
406                 strlcpy(dmt->name, "striped", DM_MAX_TYPE_NAME);
407                 dmt->init = &dm_target_stripe_init;
408                 dmt->table = &dm_target_stripe_table;
409                 dmt->strategy = &dm_target_stripe_strategy;
410                 dmt->destroy = &dm_target_stripe_destroy;
411                 dmt->upcall = &dm_target_stripe_upcall;
412                 dmt->dump = &dm_target_stripe_dump;
413
414                 err = dm_target_insert(dmt);
415                 if (err == 0)
416                         kprintf("dm_target_stripe: Successfully initialized\n");
417                 break;
418
419         case MOD_UNLOAD:
420                 err = dm_target_rem("striped");
421                 if (err == 0)
422                         kprintf("dm_target_stripe: unloaded\n");
423                 break;
424
425         default:
426                 break;
427         }
428
429         return err;
430 }
431
432 DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler);