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