dm_target_linear.c revision 1.3.4.3 1 /* $NetBSD: dm_target_linear.c,v 1.3.4.3 2009/03/03 18:30:44 skrll Exp $ */
2 >>>>>>> 1.4
3
4 /*
5 * Copyright (c) 2008 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Adam Hamsik.
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
33
34 /*
35 * This file implements initial version of device-mapper dklinear target.
36 */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40
41 #include <sys/buf.h>
42 #include <sys/kmem.h>
43 #include <sys/vnode.h>
44
45 #include <machine/int_fmtio.h>
46
47 #include "dm.h"
48
49 /*
50 * Allocate target specific config data, and link them to table.
51 * This function is called only when, flags is not READONLY and
52 * therefore we can add things to pdev list. This should not a
53 * problem because this routine is called onyl from dm_table_load_ioctl.
54 * @argv[0] is name,
55 * @argv[1] is physical data offset.
56 */
57 int
58 dm_target_linear_init(dm_dev_t *dmv, void **target_config, char *params)
59 {
60 dm_target_linear_config_t *tlc;
61 dm_pdev_t *dmp;
62
63 char **ap, *argv[3];
64
65 if(params == NULL)
66 return EINVAL;
67
68 /*
69 * Parse a string, containing tokens delimited by white space,
70 * into an argument vector
71 */
72 for (ap = argv; ap < &argv[2] &&
73 (*ap = strsep(¶ms, " \t")) != NULL;) {
74 if (**ap != '\0')
75 ap++;
76 }
77
78 /* Insert dmp to global pdev list */
79 if ((dmp = dm_pdev_insert(argv[0])) == NULL)
80 return ENOENT;
81
82 aprint_debug("Linear target init function called %s--%s!!\n",
83 argv[0], argv[1]);
84
85 if ((tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_NOSLEEP))
86 == NULL)
87 return 1;
88
89 tlc->pdev = dmp;
90 tlc->offset = 0; /* default settings */
91
92 /* Check user input if it is not leave offset as 0. */
93 tlc->offset = atoi(argv[1]);
94
95 *target_config = tlc;
96
97 dmv->dev_type = DM_LINEAR_DEV;
98
99 return 0;
100 }
101
102 /*
103 * Status routine is called to get params string, which is target
104 * specific. When dm_table_status_ioctl is called with flag
105 * DM_STATUS_TABLE_FLAG I have to sent params string back.
106 */
107 char *
108 dm_target_linear_status(void *target_config)
109 {
110 dm_target_linear_config_t *tlc;
111 char *params;
112 uint32_t i;
113 uint32_t count;
114 size_t prm_len;
115
116 tlc = target_config;
117 prm_len = 0;
118 count = 0;
119
120 /* count number of chars in offset */
121 for(i = tlc->offset; i != 0; i /= 10)
122 count++;
123
124 aprint_debug("Linear target status function called\n");
125
126 /* length of name + count of chars + one space and null char */
127 prm_len = strlen(tlc->pdev->name) + count + 2;
128
129 if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
130 return NULL;
131
132 aprint_debug("%s %"PRIu64, tlc->pdev->name, tlc->offset);
133 snprintf(params, prm_len,"%s %"PRIu64, tlc->pdev->name, tlc->offset);
134
135 return params;
136 }
137
138 /*
139 * Do IO operation, called from dmstrategy routine.
140 */
141 int
142 dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
143 {
144 dm_target_linear_config_t *tlc;
145
146 tlc = table_en->target_config;
147
148 /* printf("Linear target read function called %" PRIu64 "!!\n",
149 tlc->offset);*/
150
151 bp->b_blkno += tlc->offset;
152
153 VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
154
155 return 0;
156
157 }
158
159 /*
160 * Destroy target specific data. Decrement table pdevs.
161 */
162 int
163 dm_target_linear_destroy(dm_table_entry_t *table_en)
164 {
165 dm_target_linear_config_t *tlc;
166
167 /*
168 * Destroy function is called for every target even if it
169 * doesn't have target_config.
170 */
171
172 if (table_en->target_config == NULL)
173 return 0;
174
175 tlc = table_en->target_config;
176
177 /* Decrement pdev ref counter if 0 remove it */
178 dm_pdev_decr(tlc->pdev);
179
180 /* Unbusy target so we can unload it */
181 dm_target_unbusy(table_en->target);
182
183 kmem_free(table_en->target_config, sizeof(dm_target_linear_config_t));
184
185 table_en->target_config = NULL;
186
187 return 0;
188 }
189
190 /* Add this target pdev dependiences to prop_array_t */
191 int
192 dm_target_linear_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
193 {
194 dm_target_linear_config_t *tlc;
195 struct vattr va;
196
197 int error;
198
199 if (table_en->target_config == NULL)
200 return ENOENT;
201
202 tlc = table_en->target_config;
203
204 if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
205 return error;
206
207 prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
208
209 return 0;
210 }
211
212 /*
213 * Register upcall device.
214 * Linear target doesn't need any upcall devices but other targets like
215 * mirror, snapshot, multipath, stripe will use this functionality.
216 */
217 int
218 dm_target_linear_upcall(dm_table_entry_t *table_en, struct buf *bp)
219 {
220 return 0;
221 }
222
223 /*
224 * Transform char s to uint64_t offset number.
225 */
226 uint64_t
227 atoi(const char *s)
228 {
229 uint64_t n;
230 n = 0;
231
232 while (*s != '\0') {
233 if (!isdigit(*s))
234 break;
235
236 n = (10 * n) + (*s - '0');
237 s++;
238 }
239
240 return n;
241 }
242