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