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