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