xref: /netbsd-src/sys/dev/dm/dm_target_linear.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*        $NetBSD: dm_target_linear.c,v 1.16 2017/06/04 08:54:38 mbalmer 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 #include <sys/lwp.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 only 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(&params, " \t")) != NULL;) {
74 		if (**ap != '\0')
75 			ap++;
76 	}
77 
78 	aprint_debug("Linear target init function called %s--%s!!\n",
79 	    argv[0], argv[1]);
80 
81 	/* Insert dmp to global pdev list */
82 	if ((dmp = dm_pdev_insert(argv[0])) == NULL)
83 		return ENOENT;
84 
85 	tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP);
86 	tlc->pdev = dmp;
87 	tlc->offset = 0;	/* default settings */
88 
89 	/* Check user input if it is not leave offset as 0. */
90 	tlc->offset = atoi(argv[1]);
91 
92 	*target_config = tlc;
93 
94 	dmv->dev_type = DM_LINEAR_DEV;
95 
96 	return 0;
97 }
98 /*
99  * Status routine is called to get params string, which is target
100  * specific. When dm_table_status_ioctl is called with flag
101  * DM_STATUS_TABLE_FLAG I have to sent params string back.
102  */
103 char *
104 dm_target_linear_status(void *target_config)
105 {
106 	dm_target_linear_config_t *tlc;
107 	char *params;
108 	tlc = target_config;
109 
110 	aprint_debug("Linear target status function called\n");
111 
112 	if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_NOSLEEP)) == NULL)
113 		return NULL;
114 
115 	aprint_normal("%s %" PRIu64, tlc->pdev->name, tlc->offset);
116 	snprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
117 	    tlc->pdev->name, tlc->offset);
118 
119 	return params;
120 }
121 /*
122  * Do IO operation, called from dmstrategy routine.
123  */
124 int
125 dm_target_linear_strategy(dm_table_entry_t * table_en, struct buf * bp)
126 {
127 	dm_target_linear_config_t *tlc;
128 
129 	tlc = table_en->target_config;
130 
131 /*	printf("Linear target read function called %" PRIu64 "!!\n",
132 	tlc->offset);*/
133 
134 	bp->b_blkno += tlc->offset;
135 
136 	VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
137 
138 	return 0;
139 
140 }
141 /*
142  * Sync underlying disk caches.
143  */
144 int
145 dm_target_linear_sync(dm_table_entry_t * table_en)
146 {
147 	int cmd;
148 	dm_target_linear_config_t *tlc;
149 
150 	tlc = table_en->target_config;
151 
152 	cmd = 1;
153 
154 	return VOP_IOCTL(tlc->pdev->pdev_vnode,  DIOCCACHESYNC, &cmd,
155 	    FREAD|FWRITE, kauth_cred_get());
156 }
157 /*
158  * Destroy target specific data. Decrement table pdevs.
159  */
160 int
161 dm_target_linear_destroy(dm_table_entry_t * table_en)
162 {
163 	dm_target_linear_config_t *tlc;
164 
165 	/*
166 	 * Destroy function is called for every target even if it
167 	 * doesn't have target_config.
168 	 */
169 
170 	if (table_en->target_config == NULL)
171 		return 0;
172 
173 	tlc = table_en->target_config;
174 
175 	/* Decrement pdev ref counter if 0 remove it */
176 	dm_pdev_decr(tlc->pdev);
177 
178 	/* Unbusy target so we can unload it */
179 	dm_target_unbusy(table_en->target);
180 
181 	kmem_free(table_en->target_config, sizeof(dm_target_linear_config_t));
182 
183 	table_en->target_config = NULL;
184 
185 	return 0;
186 }
187 /* Add this target pdev dependencies to prop_array_t */
188 int
189 dm_target_linear_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
190 {
191 	dm_target_linear_config_t *tlc;
192 
193 	if (table_en->target_config == NULL)
194 		return ENOENT;
195 
196 	tlc = table_en->target_config;
197 
198 	prop_array_add_uint64(prop_array,
199 	    (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
200 
201 	return 0;
202 }
203 /*
204  * Register upcall device.
205  * Linear target doesn't need any upcall devices but other targets like
206  * mirror, snapshot, multipath, stripe will use this functionality.
207  */
208 int
209 dm_target_linear_upcall(dm_table_entry_t * table_en, struct buf * bp)
210 {
211 	return 0;
212 }
213 /*
214  * Query physical block size of this target
215  * For a linear target this is just the sector size of the underlying device
216  */
217 int
218 dm_target_linear_secsize(dm_table_entry_t * table_en, unsigned *secsizep)
219 {
220 	dm_target_linear_config_t *tlc;
221 	unsigned secsize;
222 
223 	secsize = 0;
224 
225 	tlc = table_en->target_config;
226 	if (tlc != NULL)
227 		secsize = tlc->pdev->pdev_secsize;
228 
229 	*secsizep = secsize;
230 
231 	return 0;
232 }
233 /*
234  * Transform char s to uint64_t offset number.
235  */
236 uint64_t
237 atoi(const char *s)
238 {
239 	uint64_t n;
240 	n = 0;
241 
242 	while (*s != '\0') {
243 		if (!isdigit(*s))
244 			break;
245 
246 		n = (10 * n) + (*s - '0');
247 		s++;
248 	}
249 
250 	return n;
251 }
252