dm_target_linear.c revision 1.32 1 /* $NetBSD: dm_target_linear.c,v 1.32 2019/12/15 16:14:27 tkusumi 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 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: dm_target_linear.c,v 1.32 2019/12/15 16:14:27 tkusumi Exp $");
33
34 /*
35 * This file implements initial version of device-mapper dklinear target.
36 */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/buf.h>
41 #include <sys/kmem.h>
42 #include <sys/lwp.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 only 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_table_entry_t *table_en, int argc, char **argv)
58 {
59 dm_target_linear_config_t *tlc;
60 dm_pdev_t *dmp;
61
62 /*
63 if (argc != 2) {
64 printf("Linear target takes 2 args\n");
65 return EINVAL;
66 }
67 */
68
69 aprint_debug("Linear target init function called %s--%s!!\n",
70 argv[0], argv[1]);
71
72 /* Insert dmp to global pdev list */
73 if ((dmp = dm_pdev_insert(argv[0])) == NULL)
74 return ENOENT;
75
76 tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP);
77 tlc->pdev = dmp;
78 tlc->offset = atoi64(argv[1]);
79
80 table_en->target_config = tlc;
81
82 return 0;
83 }
84
85 /*
86 * Table routine is called to get params string, which is target
87 * specific. When dm_table_status_ioctl is called with flag
88 * DM_STATUS_TABLE_FLAG I have to sent params string back.
89 */
90 char *
91 dm_target_linear_table(void *target_config)
92 {
93 dm_target_linear_config_t *tlc;
94 char *params;
95 tlc = target_config;
96
97 aprint_debug("Linear target table function called\n");
98
99 params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
100 snprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
101 tlc->pdev->name, tlc->offset);
102
103 return params;
104 }
105
106 /*
107 * Do IO operation, called from dmstrategy routine.
108 */
109 int
110 dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
111 {
112 dm_target_linear_config_t *tlc;
113
114 tlc = table_en->target_config;
115
116 bp->b_blkno += tlc->offset;
117
118 VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
119
120 return 0;
121 }
122
123 /*
124 * Sync underlying disk caches.
125 */
126 int
127 dm_target_linear_sync(dm_table_entry_t *table_en)
128 {
129 int cmd;
130 dm_target_linear_config_t *tlc;
131
132 tlc = table_en->target_config;
133
134 cmd = 1;
135
136 return VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC, &cmd,
137 FREAD|FWRITE, kauth_cred_get());
138 }
139
140 /*
141 * Destroy target specific data. Decrement table pdevs.
142 */
143 int
144 dm_target_linear_destroy(dm_table_entry_t *table_en)
145 {
146
147 /*
148 * Destroy function is called for every target even if it
149 * doesn't have target_config.
150 */
151 if (table_en->target_config == NULL)
152 goto out;
153
154 dm_target_linear_config_t *tlc = table_en->target_config;
155
156 /* Decrement pdev ref counter if 0 remove it */
157 dm_pdev_decr(tlc->pdev);
158
159 kmem_free(tlc, sizeof(*tlc));
160
161 out:
162 /* Unbusy target so we can unload it */
163 dm_target_unbusy(table_en->target);
164 return 0;
165 }
166
167 /* Add this target pdev dependencies to prop_array_t */
168 int
169 dm_target_linear_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
170 {
171 dm_target_linear_config_t *tlc;
172
173 if (table_en->target_config == NULL)
174 return ENOENT;
175
176 tlc = table_en->target_config;
177
178 prop_array_add_uint64(prop_array,
179 (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
180
181 return 0;
182 }
183
184 /*
185 * Register upcall device.
186 * Linear target doesn't need any upcall devices but other targets like
187 * mirror, snapshot, multipath, stripe will use this functionality.
188 */
189 int
190 dm_target_linear_upcall(dm_table_entry_t *table_en, struct buf *bp)
191 {
192
193 return 0;
194 }
195
196 /*
197 * Query physical block size of this target
198 * For a linear target this is just the sector size of the underlying device
199 */
200 int
201 dm_target_linear_secsize(dm_table_entry_t *table_en, unsigned int *secsizep)
202 {
203 dm_target_linear_config_t *tlc;
204 unsigned int secsize;
205
206 secsize = 0;
207
208 tlc = table_en->target_config;
209 if (tlc != NULL)
210 secsize = tlc->pdev->pdev_secsize;
211
212 *secsizep = secsize;
213
214 return 0;
215 }
216