dm_target_linear.c revision 1.3.2.2 1 1.3.2.2 mjf /* $NetBSD: dm_target_linear.c,v 1.3.2.2 2009/01/17 13:28:53 mjf Exp $ */
2 1.3.2.2 mjf
3 1.3.2.2 mjf /*
4 1.3.2.2 mjf * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.3.2.2 mjf * All rights reserved.
6 1.3.2.2 mjf *
7 1.3.2.2 mjf * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 mjf * by Adam Hamsik.
9 1.3.2.2 mjf *
10 1.3.2.2 mjf * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 mjf * modification, are permitted provided that the following conditions
12 1.3.2.2 mjf * are met:
13 1.3.2.2 mjf * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 mjf * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 mjf * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 mjf * documentation and/or other materials provided with the distribution.
18 1.3.2.2 mjf *
19 1.3.2.2 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.2.2 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.2.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.2.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.2.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.2.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.2.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.2.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.2.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.2.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.2.2 mjf * POSSIBILITY OF SUCH DAMAGE.
30 1.3.2.2 mjf */
31 1.3.2.2 mjf
32 1.3.2.2 mjf
33 1.3.2.2 mjf /*
34 1.3.2.2 mjf * This file implements initial version of device-mapper dklinear target.
35 1.3.2.2 mjf */
36 1.3.2.2 mjf
37 1.3.2.2 mjf #include <sys/types.h>
38 1.3.2.2 mjf #include <sys/param.h>
39 1.3.2.2 mjf
40 1.3.2.2 mjf #include <sys/buf.h>
41 1.3.2.2 mjf #include <sys/kmem.h>
42 1.3.2.2 mjf #include <sys/vnode.h>
43 1.3.2.2 mjf
44 1.3.2.2 mjf #include <machine/int_fmtio.h>
45 1.3.2.2 mjf
46 1.3.2.2 mjf #include "dm.h"
47 1.3.2.2 mjf
48 1.3.2.2 mjf /*
49 1.3.2.2 mjf * Allocate target specific config data, and link them to table.
50 1.3.2.2 mjf * This function is called only when, flags is not READONLY and
51 1.3.2.2 mjf * therefore we can add things to pdev list. This should not a
52 1.3.2.2 mjf * problem because this routine is called onyl from dm_table_load_ioctl.
53 1.3.2.2 mjf * @argv[0] is name,
54 1.3.2.2 mjf * @argv[1] is physical data offset.
55 1.3.2.2 mjf */
56 1.3.2.2 mjf int
57 1.3.2.2 mjf dm_target_linear_init(dm_dev_t *dmv, void **target_config, char *params)
58 1.3.2.2 mjf {
59 1.3.2.2 mjf dm_target_linear_config_t *tlc;
60 1.3.2.2 mjf dm_pdev_t *dmp;
61 1.3.2.2 mjf
62 1.3.2.2 mjf char **ap, *argv[3];
63 1.3.2.2 mjf
64 1.3.2.2 mjf if(params == NULL)
65 1.3.2.2 mjf return EINVAL;
66 1.3.2.2 mjf
67 1.3.2.2 mjf /*
68 1.3.2.2 mjf * Parse a string, containing tokens delimited by white space,
69 1.3.2.2 mjf * into an argument vector
70 1.3.2.2 mjf */
71 1.3.2.2 mjf for (ap = argv; ap < &argv[9] &&
72 1.3.2.2 mjf (*ap = strsep(¶ms, " \t")) != NULL;) {
73 1.3.2.2 mjf if (**ap != '\0')
74 1.3.2.2 mjf ap++;
75 1.3.2.2 mjf }
76 1.3.2.2 mjf
77 1.3.2.2 mjf /* Insert dmp to global pdev list */
78 1.3.2.2 mjf if ((dmp = dm_pdev_insert(argv[0])) == NULL)
79 1.3.2.2 mjf return ENOENT;
80 1.3.2.2 mjf
81 1.3.2.2 mjf aprint_debug("Linear target init function called %s--%s!!\n",
82 1.3.2.2 mjf argv[0], argv[1]);
83 1.3.2.2 mjf
84 1.3.2.2 mjf if ((tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_NOSLEEP))
85 1.3.2.2 mjf == NULL)
86 1.3.2.2 mjf return 1;
87 1.3.2.2 mjf
88 1.3.2.2 mjf tlc->pdev = dmp;
89 1.3.2.2 mjf tlc->offset = 0; /* default settings */
90 1.3.2.2 mjf
91 1.3.2.2 mjf /* Check user input if it is not leave offset as 0. */
92 1.3.2.2 mjf tlc->offset = atoi(argv[1]);
93 1.3.2.2 mjf
94 1.3.2.2 mjf *target_config = tlc;
95 1.3.2.2 mjf
96 1.3.2.2 mjf dmv->dev_type = DM_LINEAR_DEV;
97 1.3.2.2 mjf
98 1.3.2.2 mjf return 0;
99 1.3.2.2 mjf }
100 1.3.2.2 mjf
101 1.3.2.2 mjf /*
102 1.3.2.2 mjf * Status routine is called to get params string, which is target
103 1.3.2.2 mjf * specific. When dm_table_status_ioctl is called with flag
104 1.3.2.2 mjf * DM_STATUS_TABLE_FLAG I have to sent params string back.
105 1.3.2.2 mjf */
106 1.3.2.2 mjf char *
107 1.3.2.2 mjf dm_target_linear_status(void *target_config)
108 1.3.2.2 mjf {
109 1.3.2.2 mjf dm_target_linear_config_t *tlc;
110 1.3.2.2 mjf char *params;
111 1.3.2.2 mjf uint32_t i;
112 1.3.2.2 mjf uint32_t count;
113 1.3.2.2 mjf size_t prm_len;
114 1.3.2.2 mjf
115 1.3.2.2 mjf tlc = target_config;
116 1.3.2.2 mjf prm_len = 0;
117 1.3.2.2 mjf count = 0;
118 1.3.2.2 mjf
119 1.3.2.2 mjf /* count number of chars in offset */
120 1.3.2.2 mjf for(i = tlc->offset; i != 0; i /= 10)
121 1.3.2.2 mjf count++;
122 1.3.2.2 mjf
123 1.3.2.2 mjf aprint_debug("Linear target status function called\n");
124 1.3.2.2 mjf
125 1.3.2.2 mjf /* length of name + count of chars + one space and null char */
126 1.3.2.2 mjf prm_len = strlen(tlc->pdev->name) + count + 2;
127 1.3.2.2 mjf
128 1.3.2.2 mjf if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
129 1.3.2.2 mjf return NULL;
130 1.3.2.2 mjf
131 1.3.2.2 mjf aprint_debug("%s %"PRIu64, tlc->pdev->name, tlc->offset);
132 1.3.2.2 mjf snprintf(params, prm_len,"%s %"PRIu64, tlc->pdev->name, tlc->offset);
133 1.3.2.2 mjf
134 1.3.2.2 mjf return params;
135 1.3.2.2 mjf }
136 1.3.2.2 mjf
137 1.3.2.2 mjf /*
138 1.3.2.2 mjf * Do IO operation, called from dmstrategy routine.
139 1.3.2.2 mjf */
140 1.3.2.2 mjf int
141 1.3.2.2 mjf dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
142 1.3.2.2 mjf {
143 1.3.2.2 mjf dm_target_linear_config_t *tlc;
144 1.3.2.2 mjf
145 1.3.2.2 mjf tlc = table_en->target_config;
146 1.3.2.2 mjf
147 1.3.2.2 mjf /* printf("Linear target read function called %" PRIu64 "!!\n",
148 1.3.2.2 mjf tlc->offset);*/
149 1.3.2.2 mjf
150 1.3.2.2 mjf bp->b_blkno += tlc->offset;
151 1.3.2.2 mjf
152 1.3.2.2 mjf VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
153 1.3.2.2 mjf
154 1.3.2.2 mjf return 0;
155 1.3.2.2 mjf
156 1.3.2.2 mjf }
157 1.3.2.2 mjf
158 1.3.2.2 mjf /*
159 1.3.2.2 mjf * Destroy target specific data. Decrement table pdevs.
160 1.3.2.2 mjf */
161 1.3.2.2 mjf int
162 1.3.2.2 mjf dm_target_linear_destroy(dm_table_entry_t *table_en)
163 1.3.2.2 mjf {
164 1.3.2.2 mjf dm_target_linear_config_t *tlc;
165 1.3.2.2 mjf
166 1.3.2.2 mjf /*
167 1.3.2.2 mjf * Destroy function is called for every target even if it
168 1.3.2.2 mjf * doesn't have target_config.
169 1.3.2.2 mjf */
170 1.3.2.2 mjf
171 1.3.2.2 mjf if (table_en->target_config == NULL)
172 1.3.2.2 mjf return 0;
173 1.3.2.2 mjf
174 1.3.2.2 mjf tlc = table_en->target_config;
175 1.3.2.2 mjf
176 1.3.2.2 mjf /* Decrement pdev ref counter if 0 remove it */
177 1.3.2.2 mjf dm_pdev_decr(tlc->pdev);
178 1.3.2.2 mjf
179 1.3.2.2 mjf /* Unbusy target so we can unload it */
180 1.3.2.2 mjf dm_target_unbusy(table_en->target);
181 1.3.2.2 mjf
182 1.3.2.2 mjf kmem_free(table_en->target_config, sizeof(dm_target_linear_config_t));
183 1.3.2.2 mjf
184 1.3.2.2 mjf table_en->target_config = NULL;
185 1.3.2.2 mjf
186 1.3.2.2 mjf return 0;
187 1.3.2.2 mjf }
188 1.3.2.2 mjf
189 1.3.2.2 mjf /* Add this target pdev dependiences to prop_array_t */
190 1.3.2.2 mjf int
191 1.3.2.2 mjf dm_target_linear_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
192 1.3.2.2 mjf {
193 1.3.2.2 mjf dm_target_linear_config_t *tlc;
194 1.3.2.2 mjf struct vattr va;
195 1.3.2.2 mjf
196 1.3.2.2 mjf int error;
197 1.3.2.2 mjf
198 1.3.2.2 mjf if (table_en->target_config == NULL)
199 1.3.2.2 mjf return ENOENT;
200 1.3.2.2 mjf
201 1.3.2.2 mjf tlc = table_en->target_config;
202 1.3.2.2 mjf
203 1.3.2.2 mjf if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
204 1.3.2.2 mjf return error;
205 1.3.2.2 mjf
206 1.3.2.2 mjf prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
207 1.3.2.2 mjf
208 1.3.2.2 mjf return 0;
209 1.3.2.2 mjf }
210 1.3.2.2 mjf
211 1.3.2.2 mjf /*
212 1.3.2.2 mjf * Register upcall device.
213 1.3.2.2 mjf * Linear target doesn't need any upcall devices but other targets like
214 1.3.2.2 mjf * mirror, snapshot, multipath, stripe will use this functionality.
215 1.3.2.2 mjf */
216 1.3.2.2 mjf int
217 1.3.2.2 mjf dm_target_linear_upcall(dm_table_entry_t *table_en, struct buf *bp)
218 1.3.2.2 mjf {
219 1.3.2.2 mjf return 0;
220 1.3.2.2 mjf }
221 1.3.2.2 mjf
222 1.3.2.2 mjf /*
223 1.3.2.2 mjf * Transform char s to uint64_t offset number.
224 1.3.2.2 mjf */
225 1.3.2.2 mjf uint64_t
226 1.3.2.2 mjf atoi(const char *s)
227 1.3.2.2 mjf {
228 1.3.2.2 mjf uint64_t n;
229 1.3.2.2 mjf n = 0;
230 1.3.2.2 mjf
231 1.3.2.2 mjf while (*s != '\0') {
232 1.3.2.2 mjf if (!isdigit(*s))
233 1.3.2.2 mjf break;
234 1.3.2.2 mjf
235 1.3.2.2 mjf n = (10 * n) + (*s - '0');
236 1.3.2.2 mjf s++;
237 1.3.2.2 mjf }
238 1.3.2.2 mjf
239 1.3.2.2 mjf return n;
240 1.3.2.2 mjf }
241