dm_target_stripe.c revision 1.3 1 /*$NetBSD: dm_target_stripe.c,v 1.3 2009/03/01 23:15:56 haad Exp $*/
2
3 /*
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * This file implements initial version of device-mapper stripe target.
34 */
35 #include <sys/types.h>
36 #include <sys/param.h>
37
38 #include <sys/buf.h>
39 #include <sys/kmem.h>
40 #include <sys/vnode.h>
41
42 #include "dm.h"
43
44 #ifdef DM_TARGET_MODULE
45 /*
46 * Every target can be compiled directly to dm driver or as a
47 * separate module this part of target is used for loading targets
48 * to dm driver.
49 * Target can be unloaded from kernel only if there are no users of
50 * it e.g. there are no devices which uses that target.
51 */
52 #include <sys/kernel.h>
53 #include <sys/module.h>
54
55 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
56
57 static int
58 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
59 {
60 dm_target_t *dmt;
61 int r;
62 dmt = NULL;
63
64 switch (cmd) {
65 case MODULE_CMD_INIT:
66 if ((dmt = dm_target_lookup("stripe")) != NULL) {
67 dm_target_unbusy(dmt);
68 return EEXIST;
69 }
70
71 dmt = dm_target_alloc("stripe");
72
73 dmt->version[0] = 1;
74 dmt->version[1] = 0;
75 dmt->version[2] = 0;
76 strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
77 dmt->init = &dm_target_stripe_init;
78 dmt->status = &dm_target_stripe_status;
79 dmt->strategy = &dm_target_stripe_strategy;
80 dmt->deps = &dm_target_stripe_deps;
81 dmt->destroy = &dm_target_stripe_destroy;
82 dmt->upcall = &dm_target_stripe_upcall;
83
84 r = dm_target_insert(dmt);
85
86 break;
87
88 case MODULE_CMD_FINI:
89 r = dm_target_rem("stripe");
90 break;
91
92 case MODULE_CMD_STAT:
93 return ENOTTY;
94
95 default:
96 return ENOTTY;
97 }
98
99 return r;
100 }
101
102 #endif
103
104 /*
105 * Init function called from dm_table_load_ioctl.
106 * Example line sent to dm from lvm tools when using striped target.
107 * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
108 * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
109 */
110 int
111 dm_target_stripe_init(dm_dev_t *dmv, void **target_config, char *params)
112 {
113 dm_target_stripe_config_t *tsc;
114 size_t len;
115 char **ap, *argv[10];
116
117 if(params == NULL)
118 return EINVAL;
119
120 len = strlen(params) + 1;
121
122 /*
123 * Parse a string, containing tokens delimited by white space,
124 * into an argument vector
125 */
126 for (ap = argv; ap < &argv[9] &&
127 (*ap = strsep(¶ms, " \t")) != NULL;) {
128 if (**ap != '\0')
129 ap++;
130 }
131
132 printf("Stripe target init function called!!\n");
133
134 printf("Stripe target chunk size %s number of stripes %s\n", argv[1], argv[0]);
135 printf("Stripe target device name %s -- offset %s\n", argv[2], argv[3]);
136 printf("Stripe target device name %s -- offset %s\n", argv[4], argv[5]);
137
138 if (atoi(argv[0]) > MAX_STRIPES)
139 return ENOTSUP;
140
141 if ((tsc = kmem_alloc(sizeof(dm_target_stripe_config_t), KM_NOSLEEP))
142 == NULL)
143 return ENOMEM;
144
145 /* Insert dmp to global pdev list */
146 if ((tsc->stripe_devs[0].pdev = dm_pdev_insert(argv[2])) == NULL)
147 return ENOENT;
148
149 /* Insert dmp to global pdev list */
150 if ((tsc->stripe_devs[1].pdev = dm_pdev_insert(argv[4])) == NULL)
151 return ENOENT;
152
153 tsc->stripe_devs[0].offset = atoi(argv[3]);
154 tsc->stripe_devs[1].offset = atoi(argv[5]);
155
156 /* Save length of param string */
157 tsc->params_len = len;
158 tsc->stripe_chunksize = atoi(argv[1]);
159 tsc->stripe_num = (uint8_t)atoi(argv[0]);
160
161 *target_config = tsc;
162
163 dmv->dev_type = DM_STRIPE_DEV;
164
165 return 0;
166 }
167
168 /* Status routine called to get params string. */
169 char *
170 dm_target_stripe_status(void *target_config)
171 {
172 dm_target_stripe_config_t *tsc;
173 char *params;
174
175 tsc = target_config;
176
177 if ((params = kmem_alloc(tsc->params_len, KM_NOSLEEP)) == NULL)
178 return NULL;
179
180 snprintf(params, tsc->params_len, "%d %lld %s %lld %s %lld", tsc->stripe_num, tsc->stripe_chunksize,
181 tsc->stripe_devs[0].pdev->name, tsc->stripe_devs[0].offset,
182 tsc->stripe_devs[1].pdev->name, tsc->stripe_devs[1].offset);
183
184 return params;
185 }
186
187 /* Strategy routine called from dm_strategy. */
188 int
189 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
190 {
191
192 bp->b_error = EIO;
193 bp->b_resid = 0;
194
195 biodone(bp);
196
197 return 0;
198 }
199
200 /* Doesn't do anything here. */
201 int
202 dm_target_stripe_destroy(dm_table_entry_t *table_en)
203 {
204 dm_target_stripe_config_t *tsc;
205
206 tsc = table_en->target_config;
207
208 if (tsc == NULL)
209 return 0;
210
211 dm_pdev_decr(tsc->stripe_devs[0].pdev);
212 dm_pdev_decr(tsc->stripe_devs[1].pdev);
213
214 /* Unbusy target so we can unload it */
215 dm_target_unbusy(table_en->target);
216
217 kmem_free(tsc, sizeof(dm_target_stripe_config_t));
218
219 table_en->target_config = NULL;
220
221 return 0;
222 }
223
224 /* Doesn't not need to do anything here. */
225 int
226 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
227 {
228 dm_target_stripe_config_t *tsc;
229 struct vattr va;
230
231 int error;
232
233 if (table_en->target_config == NULL)
234 return ENOENT;
235
236 tsc = table_en->target_config;
237
238 if ((error = VOP_GETATTR(tsc->stripe_devs[0].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
239 return error;
240
241 prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
242
243 if ((error = VOP_GETATTR(tsc->stripe_devs[1].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
244 return error;
245
246 prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
247
248 return 0;
249 }
250
251 /* Unsupported for this target. */
252 int
253 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
254 {
255 return 0;
256 }
257