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