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