device-mapper.c revision 1.1.2.12 1 1.1.2.12 haad /* $NetBSD: device-mapper.c,v 1.1.2.12 2008/09/10 18:43:27 haad Exp $ */
2 1.1.2.7 haad
3 1.1.2.1 haad /*
4 1.1.2.1 haad * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 1.1.2.1 haad * All rights reserved.
6 1.1.2.1 haad *
7 1.1.2.1 haad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 haad * by Adam Hamsik.
9 1.1.2.1 haad *
10 1.1.2.1 haad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 haad * modification, are permitted provided that the following conditions
12 1.1.2.1 haad * are met:
13 1.1.2.1 haad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 haad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 haad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 haad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 haad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 haad *
19 1.1.2.1 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 haad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 haad */
31 1.1.2.1 haad
32 1.1.2.1 haad /*
33 1.1.2.2 haad * I want to say thank you to all people who helped me with this project.
34 1.1.2.1 haad */
35 1.1.2.1 haad
36 1.1.2.1 haad #include <sys/types.h>
37 1.1.2.1 haad #include <sys/param.h>
38 1.1.2.1 haad
39 1.1.2.10 haad #include <sys/atomic.h>
40 1.1.2.1 haad #include <sys/buf.h>
41 1.1.2.1 haad #include <sys/conf.h>
42 1.1.2.1 haad #include <sys/dkio.h>
43 1.1.2.1 haad #include <sys/disk.h>
44 1.1.2.1 haad #include <sys/disklabel.h>
45 1.1.2.1 haad #include <sys/ioctl.h>
46 1.1.2.1 haad #include <sys/ioccom.h>
47 1.1.2.1 haad #include <sys/kmem.h>
48 1.1.2.6 haad #include <sys/module.h>
49 1.1.2.10 haad #include <sys/mutex.h>
50 1.1.2.10 haad
51 1.1.2.1 haad
52 1.1.2.1 haad #include "netbsd-dm.h"
53 1.1.2.1 haad #include "dm.h"
54 1.1.2.1 haad
55 1.1.2.1 haad static dev_type_open(dmopen);
56 1.1.2.1 haad static dev_type_close(dmclose);
57 1.1.2.1 haad static dev_type_read(dmread);
58 1.1.2.1 haad static dev_type_write(dmwrite);
59 1.1.2.1 haad static dev_type_ioctl(dmioctl);
60 1.1.2.1 haad static dev_type_strategy(dmstrategy);
61 1.1.2.1 haad static dev_type_dump(dmdump);
62 1.1.2.1 haad static dev_type_size(dmsize);
63 1.1.2.1 haad
64 1.1.2.1 haad /* attach and detach routines */
65 1.1.2.1 haad int dmattach(void);
66 1.1.2.1 haad int dmdestroy(void);
67 1.1.2.1 haad
68 1.1.2.1 haad static int dm_cmd_to_fun(prop_dictionary_t);
69 1.1.2.1 haad static int disk_ioctl_switch(dev_t, u_long, void *);
70 1.1.2.1 haad static int dm_ioctl_switch(u_long);
71 1.1.2.3 haad static void dmminphys(struct buf *);
72 1.1.2.11 haad static void dmgetdisklabel(struct disklabel **, dev_t);
73 1.1.2.3 haad /* Called to initialize disklabel values for readdisklabel. */
74 1.1.2.11 haad static void dmgetdefaultdisklabel(struct disklabel *, dev_t);
75 1.1.2.3 haad
76 1.1.2.1 haad /* ***Variable-definitions*** */
77 1.1.2.1 haad const struct bdevsw dm_bdevsw = {
78 1.1.2.1 haad dmopen, dmclose, dmstrategy, dmioctl, dmdump, dmsize,
79 1.1.2.1 haad D_DISK
80 1.1.2.1 haad };
81 1.1.2.1 haad
82 1.1.2.1 haad const struct cdevsw dm_cdevsw = {
83 1.1.2.1 haad dmopen, dmclose, dmread, dmwrite, dmioctl,
84 1.1.2.6 haad nostop, notty, nopoll, nommap, nokqfilter, D_DISK
85 1.1.2.1 haad };
86 1.1.2.1 haad
87 1.1.2.1 haad /* Info about all devices */
88 1.1.2.1 haad struct dm_softc *dm_sc;
89 1.1.2.1 haad
90 1.1.2.10 haad kmutex_t dm_ioctl_mtx;
91 1.1.2.10 haad
92 1.1.2.1 haad /*
93 1.1.2.1 haad * This array is used to translate cmd to function pointer.
94 1.1.2.1 haad *
95 1.1.2.1 haad * Interface between libdevmapper and lvm2tools uses different
96 1.1.2.1 haad * names for one IOCTL call because libdevmapper do another thing
97 1.1.2.1 haad * then. When I run "info" or "mknodes" libdevmapper will send same
98 1.1.2.1 haad * ioctl to kernel but will do another things in userspace.
99 1.1.2.1 haad *
100 1.1.2.1 haad */
101 1.1.2.1 haad struct cmd_function cmd_fn[] = {
102 1.1.2.1 haad {"version", dm_get_version_ioctl},
103 1.1.2.1 haad {"targets", dm_list_versions_ioctl},
104 1.1.2.1 haad {"create", dm_dev_create_ioctl},
105 1.1.2.1 haad {"info", dm_dev_status_ioctl},
106 1.1.2.1 haad {"mknodes", dm_dev_status_ioctl},
107 1.1.2.1 haad {"names", dm_dev_list_ioctl},
108 1.1.2.1 haad {"suspend", dm_dev_suspend_ioctl},
109 1.1.2.1 haad {"remove", dm_dev_remove_ioctl},
110 1.1.2.1 haad {"rename", dm_dev_rename_ioctl},
111 1.1.2.1 haad {"resume", dm_dev_resume_ioctl},
112 1.1.2.1 haad {"clear", dm_table_clear_ioctl},
113 1.1.2.1 haad {"deps", dm_table_deps_ioctl},
114 1.1.2.1 haad {"reload", dm_table_load_ioctl},
115 1.1.2.1 haad {"status", dm_table_status_ioctl},
116 1.1.2.1 haad {"table", dm_table_status_ioctl},
117 1.1.2.1 haad {NULL, NULL}
118 1.1.2.1 haad };
119 1.1.2.1 haad
120 1.1.2.6 haad
121 1.1.2.6 haad MODULE(MODULE_CLASS_MISC, dm, NULL);
122 1.1.2.6 haad
123 1.1.2.6 haad /* New module handle routine */
124 1.1.2.6 haad static int
125 1.1.2.6 haad dm_modcmd(modcmd_t cmd, void *arg)
126 1.1.2.6 haad {
127 1.1.2.10 haad #ifdef _MODULE
128 1.1.2.8 haad int bmajor = -1, cmajor = -1;
129 1.1.2.8 haad
130 1.1.2.6 haad switch (cmd) {
131 1.1.2.6 haad case MODULE_CMD_INIT:
132 1.1.2.6 haad dmattach();
133 1.1.2.8 haad return devsw_attach("dm", &dm_bdevsw, &bmajor,
134 1.1.2.8 haad &dm_cdevsw, &cmajor);
135 1.1.2.6 haad break;
136 1.1.2.6 haad
137 1.1.2.6 haad case MODULE_CMD_FINI:
138 1.1.2.6 haad dmdestroy();
139 1.1.2.8 haad return devsw_detach(&dm_bdevsw, &dm_cdevsw);
140 1.1.2.6 haad break;
141 1.1.2.6 haad
142 1.1.2.6 haad case MODULE_CMD_STAT:
143 1.1.2.6 haad return ENOTTY;
144 1.1.2.6 haad
145 1.1.2.6 haad default:
146 1.1.2.6 haad return ENOTTY;
147 1.1.2.6 haad }
148 1.1.2.6 haad
149 1.1.2.6 haad return 0;
150 1.1.2.6 haad #else
151 1.1.2.6 haad
152 1.1.2.10 haad if (cmd == MODULE_CMD_INIT)
153 1.1.2.8 haad return 0;
154 1.1.2.6 haad return ENOTTY;
155 1.1.2.10 haad
156 1.1.2.6 haad #endif /* _MODULE */
157 1.1.2.6 haad }
158 1.1.2.6 haad
159 1.1.2.6 haad
160 1.1.2.1 haad /* attach routine */
161 1.1.2.1 haad int
162 1.1.2.1 haad dmattach(void)
163 1.1.2.1 haad {
164 1.1.2.12 haad dm_sc = (struct dm_softc *)kmem_zalloc(sizeof(struct dm_softc), KM_NOSLEEP);
165 1.1.2.1 haad
166 1.1.2.1 haad if (dm_sc == NULL){
167 1.1.2.1 haad aprint_error("Not enough memory for dm device.\n");
168 1.1.2.12 haad return ENOMEM;
169 1.1.2.1 haad }
170 1.1.2.1 haad
171 1.1.2.1 haad dm_sc->sc_minor_num = 0;
172 1.1.2.1 haad dm_sc->sc_ref_count = 0;
173 1.1.2.1 haad
174 1.1.2.1 haad dm_target_init();
175 1.1.2.1 haad
176 1.1.2.1 haad dm_dev_init();
177 1.1.2.1 haad
178 1.1.2.1 haad dm_pdev_init();
179 1.1.2.1 haad
180 1.1.2.10 haad mutex_init(&dm_ioctl_mtx, MUTEX_DEFAULT, IPL_NONE);
181 1.1.2.10 haad
182 1.1.2.1 haad return 0;
183 1.1.2.1 haad }
184 1.1.2.1 haad
185 1.1.2.1 haad /* Destroy routine */
186 1.1.2.1 haad int
187 1.1.2.1 haad dmdestroy(void)
188 1.1.2.1 haad {
189 1.1.2.2 haad (void)kmem_free(dm_sc, sizeof(struct dm_softc));
190 1.1.2.1 haad
191 1.1.2.9 haad dm_dev_destroy();
192 1.1.2.9 haad
193 1.1.2.9 haad dm_pdev_destroy();
194 1.1.2.9 haad
195 1.1.2.9 haad dm_target_destroy();
196 1.1.2.9 haad
197 1.1.2.10 haad mutex_destroy(&dm_ioctl_mtx);
198 1.1.2.10 haad
199 1.1.2.1 haad return 0;
200 1.1.2.1 haad }
201 1.1.2.1 haad
202 1.1.2.1 haad static int
203 1.1.2.1 haad dmopen(dev_t dev, int flags, int mode, struct lwp *l)
204 1.1.2.1 haad {
205 1.1.2.1 haad
206 1.1.2.1 haad struct dm_dev *dmv;
207 1.1.2.1 haad
208 1.1.2.10 haad aprint_verbose("open routine called %d\n", minor(dev));
209 1.1.2.10 haad
210 1.1.2.12 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) != NULL)
211 1.1.2.1 haad dmv->ref_cnt++;
212 1.1.2.11 haad
213 1.1.2.1 haad return 0;
214 1.1.2.1 haad }
215 1.1.2.1 haad
216 1.1.2.1 haad
217 1.1.2.1 haad static int
218 1.1.2.1 haad dmclose(dev_t dev, int flags, int mode, struct lwp *l)
219 1.1.2.1 haad {
220 1.1.2.1 haad struct dm_dev *dmv;
221 1.1.2.1 haad
222 1.1.2.12 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) != NULL)
223 1.1.2.10 haad dmv->ref_cnt--;
224 1.1.2.1 haad
225 1.1.2.1 haad aprint_verbose("CLOSE routine called\n");
226 1.1.2.1 haad
227 1.1.2.1 haad return 0;
228 1.1.2.1 haad }
229 1.1.2.1 haad
230 1.1.2.1 haad /*
231 1.1.2.1 haad * Called after ioctl call on mapper/control or dm device.
232 1.1.2.10 haad * Locking: Use dm_ioctl_mtx to synchronise access to dm driver.
233 1.1.2.10 haad * Only one ioctl can be in dm driver in time. Ioctl's are not
234 1.1.2.10 haad * run many times and they performance is not critical, therefore
235 1.1.2.10 haad * I can do it this way.
236 1.1.2.1 haad */
237 1.1.2.1 haad static int
238 1.1.2.1 haad dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
239 1.1.2.1 haad {
240 1.1.2.1 haad int r;
241 1.1.2.1 haad prop_dictionary_t dm_dict_in;
242 1.1.2.1 haad
243 1.1.2.1 haad r = 0;
244 1.1.2.1 haad
245 1.1.2.1 haad if (data == NULL)
246 1.1.2.1 haad return(EINVAL);
247 1.1.2.10 haad
248 1.1.2.10 haad mutex_enter(&dm_ioctl_mtx);
249 1.1.2.10 haad
250 1.1.2.2 haad if (disk_ioctl_switch(dev, cmd, data) != 0) {
251 1.1.2.1 haad struct plistref *pref = (struct plistref *) data;
252 1.1.2.1 haad
253 1.1.2.1 haad r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in);
254 1.1.2.1 haad if (r)
255 1.1.2.10 haad goto out;
256 1.1.2.10 haad
257 1.1.2.1 haad dm_check_version(dm_dict_in);
258 1.1.2.1 haad
259 1.1.2.1 haad /* call cmd selected function */
260 1.1.2.1 haad r = dm_ioctl_switch(cmd);
261 1.1.2.11 haad if (r)
262 1.1.2.1 haad goto out;
263 1.1.2.1 haad
264 1.1.2.12 haad /* char *xml;
265 1.1.2.1 haad xml = prop_dictionary_externalize(dm_dict_in);
266 1.1.2.12 haad aprint_verbose("%s\n",xml);*/
267 1.1.2.1 haad
268 1.1.2.1 haad r = dm_cmd_to_fun(dm_dict_in);
269 1.1.2.11 haad if (r)
270 1.1.2.1 haad goto out;
271 1.1.2.1 haad
272 1.1.2.1 haad r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
273 1.1.2.1 haad }
274 1.1.2.11 haad
275 1.1.2.1 haad out:
276 1.1.2.10 haad mutex_exit(&dm_ioctl_mtx);
277 1.1.2.1 haad return r;
278 1.1.2.1 haad }
279 1.1.2.10 haad
280 1.1.2.2 haad /*
281 1.1.2.2 haad * Translate command sent from libdevmapper to func.
282 1.1.2.2 haad */
283 1.1.2.1 haad static int
284 1.1.2.1 haad dm_cmd_to_fun(prop_dictionary_t dm_dict){
285 1.1.2.1 haad int i,len,slen;
286 1.1.2.1 haad int r;
287 1.1.2.1 haad const char *command;
288 1.1.2.1 haad
289 1.1.2.1 haad r = 0;
290 1.1.2.1 haad
291 1.1.2.2 haad (void)prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_COMMAND,
292 1.1.2.2 haad &command);
293 1.1.2.1 haad
294 1.1.2.1 haad len = strlen(command);
295 1.1.2.1 haad
296 1.1.2.12 haad for(i=0; cmd_fn[i].cmd != NULL; i++){
297 1.1.2.1 haad slen = strlen(cmd_fn[i].cmd);
298 1.1.2.1 haad
299 1.1.2.1 haad if (len != slen)
300 1.1.2.1 haad continue;
301 1.1.2.1 haad
302 1.1.2.2 haad if ((strncmp(command, cmd_fn[i].cmd, slen)) == 0) {
303 1.1.2.12 haad printf("ioctl command: %s\n", command);
304 1.1.2.1 haad r = cmd_fn[i].fn(dm_dict);
305 1.1.2.1 haad break;
306 1.1.2.1 haad }
307 1.1.2.1 haad }
308 1.1.2.1 haad
309 1.1.2.1 haad return r;
310 1.1.2.1 haad }
311 1.1.2.1 haad
312 1.1.2.1 haad /* Call apropriate ioctl handler function. */
313 1.1.2.1 haad static int
314 1.1.2.1 haad dm_ioctl_switch(u_long cmd)
315 1.1.2.1 haad {
316 1.1.2.1 haad int r;
317 1.1.2.1 haad
318 1.1.2.1 haad r = 0;
319 1.1.2.1 haad
320 1.1.2.1 haad switch(cmd) {
321 1.1.2.1 haad
322 1.1.2.1 haad case NETBSD_DM_IOCTL:
323 1.1.2.11 haad aprint_debug("NetBSD_DM_IOCTL called\n");
324 1.1.2.1 haad break;
325 1.1.2.1 haad
326 1.1.2.1 haad default:
327 1.1.2.11 haad aprint_debug("unknown ioctl called\n");
328 1.1.2.11 haad return ENOTTY;
329 1.1.2.1 haad break; /* NOT REACHED */
330 1.1.2.1 haad }
331 1.1.2.1 haad
332 1.1.2.1 haad return r;
333 1.1.2.1 haad }
334 1.1.2.1 haad
335 1.1.2.1 haad /*
336 1.1.2.1 haad * Check for disk specific ioctls.
337 1.1.2.1 haad */
338 1.1.2.1 haad
339 1.1.2.10 haad static int
340 1.1.2.10 haad disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
341 1.1.2.10 haad {
342 1.1.2.1 haad switch(cmd) {
343 1.1.2.1 haad
344 1.1.2.12 haad /*case DIOCGWEDGEINFO:
345 1.1.2.1 haad {
346 1.1.2.1 haad struct dkwedge_info *dkw = (void *) data;
347 1.1.2.1 haad
348 1.1.2.11 haad aprint_debug("DIOCGWEDGEINFO ioctl called\n");
349 1.1.2.1 haad
350 1.1.2.1 haad strlcpy(dkw->dkw_devname, dmv->name, 16);
351 1.1.2.1 haad strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
352 1.1.2.1 haad strlcpy(dkw->dkw_parent, dmv->name, 16);
353 1.1.2.1 haad
354 1.1.2.1 haad dkw->dkw_offset = 0;
355 1.1.2.1 haad dkw->dkw_size = dmsize(dev);
356 1.1.2.1 haad strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
357 1.1.2.1 haad
358 1.1.2.1 haad break;
359 1.1.2.12 haad }*/
360 1.1.2.1 haad
361 1.1.2.1 haad case DIOCGDINFO:
362 1.1.2.11 haad {
363 1.1.2.11 haad struct disklabel *dk_label;
364 1.1.2.11 haad dk_label = NULL;
365 1.1.2.11 haad aprint_debug("DIOCGDINFO called\n");
366 1.1.2.11 haad dmgetdisklabel(&dk_label, dev);
367 1.1.2.11 haad
368 1.1.2.11 haad *(struct disklabel *)data = *(dk_label);
369 1.1.2.1 haad break;
370 1.1.2.11 haad }
371 1.1.2.11 haad
372 1.1.2.11 haad case DIOCGPART:
373 1.1.2.11 haad {
374 1.1.2.11 haad struct disklabel *dk_label;
375 1.1.2.11 haad dk_label = NULL;
376 1.1.2.11 haad aprint_debug("DIOCGDINFO called\n");
377 1.1.2.11 haad dmgetdisklabel(&dk_label, dev);
378 1.1.2.1 haad
379 1.1.2.11 haad ((struct partinfo *)data)->disklab = dk_label;
380 1.1.2.11 haad ((struct partinfo *)data)->part = &dk_label->d_partitions[DISKPART(dev)];
381 1.1.2.11 haad break;
382 1.1.2.11 haad }
383 1.1.2.1 haad case DIOCWDINFO:
384 1.1.2.1 haad case DIOCSDINFO:
385 1.1.2.1 haad case DIOCKLABEL:
386 1.1.2.1 haad case DIOCWLABEL:
387 1.1.2.1 haad case DIOCGDEFLABEL:
388 1.1.2.1 haad
389 1.1.2.1 haad default:
390 1.1.2.1 haad aprint_verbose("unknown disk_ioctl called\n");
391 1.1.2.1 haad return 1;
392 1.1.2.1 haad break; /* NOT REACHED */
393 1.1.2.1 haad }
394 1.1.2.1 haad
395 1.1.2.1 haad return 0;
396 1.1.2.10 haad }
397 1.1.2.1 haad
398 1.1.2.2 haad /*
399 1.1.2.2 haad * Do all IO operations on dm logical devices.
400 1.1.2.2 haad */
401 1.1.2.10 haad static void
402 1.1.2.1 haad dmstrategy(struct buf *bp)
403 1.1.2.1 haad {
404 1.1.2.1 haad
405 1.1.2.1 haad struct dm_dev *dmv;
406 1.1.2.1 haad struct dm_table *tbl;
407 1.1.2.1 haad
408 1.1.2.1 haad struct dm_table_entry *table_en;
409 1.1.2.1 haad
410 1.1.2.1 haad struct buf *nestbuf;
411 1.1.2.1 haad
412 1.1.2.10 haad uint32_t dev_type;
413 1.1.2.10 haad
414 1.1.2.1 haad uint64_t table_start;
415 1.1.2.1 haad uint64_t table_end;
416 1.1.2.1 haad
417 1.1.2.1 haad uint64_t buf_start;
418 1.1.2.1 haad uint64_t buf_len;
419 1.1.2.1 haad
420 1.1.2.1 haad uint64_t start;
421 1.1.2.1 haad uint64_t end;
422 1.1.2.1 haad
423 1.1.2.1 haad uint64_t issued_len;
424 1.1.2.1 haad
425 1.1.2.1 haad buf_start = bp->b_blkno * DEV_BSIZE;
426 1.1.2.1 haad buf_len = bp->b_bcount;
427 1.1.2.1 haad
428 1.1.2.10 haad tbl = NULL;
429 1.1.2.1 haad
430 1.1.2.1 haad table_end = 0;
431 1.1.2.10 haad dev_type = 0;
432 1.1.2.1 haad issued_len = 0;
433 1.1.2.1 haad
434 1.1.2.10 haad /* dm_dev are guarded by their own mutex */
435 1.1.2.12 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
436 1.1.2.1 haad bp->b_error = EIO;
437 1.1.2.1 haad bp->b_resid = bp->b_bcount;
438 1.1.2.1 haad biodone(bp);
439 1.1.2.1 haad return;
440 1.1.2.1 haad }
441 1.1.2.10 haad /*
442 1.1.2.10 haad * Test if deleting flag is not set if it is set fail io
443 1.1.2.10 haad * operation. There is small window between rw_enter and
444 1.1.2.10 haad * dev_rem in dm_dev_remove_ioctl when new IO can be started
445 1.1.2.10 haad * on device (it will wait on dev_rwlock) which will be
446 1.1.2.10 haad * destroyed.
447 1.1.2.10 haad */
448 1.1.2.10 haad dev_type = atomic_and_32_nv(&dmv->dev_type, DM_DELETING_DEV);
449 1.1.2.10 haad
450 1.1.2.10 haad if (dev_type & DM_DELETING_DEV) {
451 1.1.2.10 haad bp->b_error = EIO;
452 1.1.2.10 haad bp->b_resid = bp->b_bcount;
453 1.1.2.10 haad biodone(bp);
454 1.1.2.10 haad return;
455 1.1.2.10 haad }
456 1.1.2.1 haad
457 1.1.2.10 haad /* Read lock per device rwlock so device can't be changed. */
458 1.1.2.4 haad rw_enter(&dmv->dev_rwlock, RW_READER);
459 1.1.2.4 haad
460 1.1.2.1 haad /* Select active table */
461 1.1.2.1 haad tbl = &dmv->tables[dmv->cur_active_table];
462 1.1.2.1 haad
463 1.1.2.1 haad /* Nested buffers count down to zero therefore I have
464 1.1.2.1 haad to set bp->b_resid to maximal value. */
465 1.1.2.1 haad bp->b_resid = bp->b_bcount;
466 1.1.2.1 haad
467 1.1.2.1 haad /*
468 1.1.2.1 haad * Find out what tables I want to select.
469 1.1.2.1 haad */
470 1.1.2.1 haad SLIST_FOREACH(table_en, tbl, next)
471 1.1.2.1 haad {
472 1.1.2.1 haad
473 1.1.2.1 haad /* I need need number of bytes not blocks. */
474 1.1.2.1 haad table_start = table_en->start * DEV_BSIZE;
475 1.1.2.2 haad /*
476 1.1.2.2 haad * I have to sub 1 from table_en->length to prevent
477 1.1.2.2 haad * off by one error
478 1.1.2.2 haad */
479 1.1.2.1 haad table_end = table_start + (table_en->length)* DEV_BSIZE;
480 1.1.2.1 haad
481 1.1.2.1 haad start = MAX(table_start, buf_start);
482 1.1.2.1 haad
483 1.1.2.1 haad end = MIN(table_end, buf_start + buf_len);
484 1.1.2.1 haad
485 1.1.2.1 haad aprint_debug("----------------------------------------\n");
486 1.1.2.2 haad aprint_debug("table_start %010" PRIu64", table_end %010"
487 1.1.2.2 haad PRIu64 "\n", table_start, table_end);
488 1.1.2.2 haad aprint_debug("buf_start %010" PRIu64", buf_len %010"
489 1.1.2.2 haad PRIu64"\n", buf_start, buf_len);
490 1.1.2.2 haad aprint_debug("start-buf_start %010"PRIu64", end %010"
491 1.1.2.2 haad PRIu64"\n", start - buf_start, end);
492 1.1.2.1 haad aprint_debug("end-start %010" PRIu64 "\n", end - start);
493 1.1.2.1 haad aprint_debug("\n----------------------------------------\n");
494 1.1.2.1 haad
495 1.1.2.1 haad if (start < end) {
496 1.1.2.1 haad /* create nested buffer */
497 1.1.2.1 haad nestbuf = getiobuf(NULL, true);
498 1.1.2.1 haad
499 1.1.2.2 haad nestiobuf_setup(bp, nestbuf, start - buf_start,
500 1.1.2.2 haad (end-start));
501 1.1.2.1 haad
502 1.1.2.1 haad issued_len += end-start;
503 1.1.2.1 haad
504 1.1.2.1 haad /* I need number of blocks. */
505 1.1.2.1 haad nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
506 1.1.2.1 haad
507 1.1.2.1 haad table_en->target->strategy(table_en, nestbuf);
508 1.1.2.1 haad }
509 1.1.2.1 haad }
510 1.1.2.1 haad
511 1.1.2.1 haad if (issued_len < buf_len)
512 1.1.2.1 haad nestiobuf_done(bp, buf_len - issued_len, EINVAL);
513 1.1.2.4 haad
514 1.1.2.4 haad rw_exit(&dmv->dev_rwlock);
515 1.1.2.1 haad
516 1.1.2.1 haad return;
517 1.1.2.1 haad }
518 1.1.2.1 haad
519 1.1.2.1 haad
520 1.1.2.1 haad static int
521 1.1.2.1 haad dmread(dev_t dev, struct uio *uio, int flag)
522 1.1.2.1 haad {
523 1.1.2.1 haad return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
524 1.1.2.1 haad }
525 1.1.2.1 haad
526 1.1.2.1 haad static int
527 1.1.2.1 haad dmwrite(dev_t dev, struct uio *uio, int flag)
528 1.1.2.1 haad {
529 1.1.2.1 haad return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
530 1.1.2.1 haad }
531 1.1.2.1 haad
532 1.1.2.1 haad static int
533 1.1.2.1 haad dmdump(dev_t dev, daddr_t blkno, void *va, size_t size)
534 1.1.2.1 haad {
535 1.1.2.1 haad return ENODEV;
536 1.1.2.1 haad }
537 1.1.2.1 haad
538 1.1.2.1 haad static int
539 1.1.2.1 haad dmsize(dev_t dev)
540 1.1.2.1 haad {
541 1.1.2.1 haad struct dm_dev *dmv;
542 1.1.2.1 haad struct dm_table *tbl;
543 1.1.2.1 haad struct dm_table_entry *table_en;
544 1.1.2.1 haad
545 1.1.2.1 haad uint64_t length;
546 1.1.2.1 haad
547 1.1.2.1 haad length = 0;
548 1.1.2.1 haad
549 1.1.2.12 haad if ( (dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
550 1.1.2.1 haad return ENODEV;
551 1.1.2.1 haad
552 1.1.2.1 haad /* Select active table */
553 1.1.2.1 haad tbl = &dmv->tables[dmv->cur_active_table];
554 1.1.2.10 haad
555 1.1.2.10 haad rw_enter(&dmv->dev_rwlock, RW_READER);
556 1.1.2.10 haad
557 1.1.2.1 haad /*
558 1.1.2.1 haad * Find out what tables I want to select.
559 1.1.2.1 haad * if length => rawblkno then we should used that table.
560 1.1.2.1 haad */
561 1.1.2.1 haad SLIST_FOREACH(table_en, tbl, next)
562 1.1.2.1 haad length += table_en->length;
563 1.1.2.1 haad
564 1.1.2.10 haad rw_exit(&dmv->dev_rwlock);
565 1.1.2.10 haad
566 1.1.2.11 haad return length / DEV_BSIZE;
567 1.1.2.1 haad }
568 1.1.2.1 haad
569 1.1.2.1 haad static void
570 1.1.2.1 haad dmminphys(struct buf *bp)
571 1.1.2.1 haad {
572 1.1.2.1 haad bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
573 1.1.2.1 haad }
574 1.1.2.11 haad
575 1.1.2.1 haad /*
576 1.1.2.1 haad * Load the label information on the named device
577 1.1.2.1 haad * Actually fabricate a disklabel
578 1.1.2.1 haad *
579 1.1.2.1 haad * EVENTUALLY take information about different
580 1.1.2.1 haad * data tracks from the TOC and put it in the disklabel
581 1.1.2.11 haad *
582 1.1.2.11 haad * Copied from vnd code.
583 1.1.2.1 haad */
584 1.1.2.1 haad static void
585 1.1.2.11 haad dmgetdisklabel(struct disklabel **dk_label, dev_t dev)
586 1.1.2.1 haad {
587 1.1.2.11 haad if ((*dk_label = kmem_zalloc(sizeof(struct disklabel), KM_NOSLEEP))
588 1.1.2.3 haad == NULL)
589 1.1.2.3 haad return;
590 1.1.2.3 haad
591 1.1.2.11 haad dmgetdefaultdisklabel(*dk_label, dev);
592 1.1.2.3 haad
593 1.1.2.3 haad return;
594 1.1.2.3 haad }
595 1.1.2.3 haad
596 1.1.2.3 haad /*
597 1.1.2.3 haad * Initialize disklabel values, so we can use it for readdisklabel.
598 1.1.2.3 haad */
599 1.1.2.3 haad static void
600 1.1.2.11 haad dmgetdefaultdisklabel(struct disklabel *lp, dev_t dev)
601 1.1.2.3 haad {
602 1.1.2.3 haad struct partition *pp;
603 1.1.2.3 haad int dmp_size;
604 1.1.2.1 haad
605 1.1.2.3 haad dmp_size = dmsize(dev);
606 1.1.2.1 haad
607 1.1.2.1 haad /*
608 1.1.2.3 haad * Size must be at least 2048 DEV_BSIZE blocks
609 1.1.2.3 haad * (1M) in order to use this geometry.
610 1.1.2.1 haad */
611 1.1.2.3 haad
612 1.1.2.3 haad lp->d_secperunit = dmp_size;
613 1.1.2.3 haad lp->d_secsize = DEV_BSIZE;
614 1.1.2.3 haad lp->d_nsectors = 32;
615 1.1.2.3 haad lp->d_ntracks = 64;
616 1.1.2.3 haad lp->d_ncylinders = dmp_size / (lp->d_nsectors * lp->d_ntracks);
617 1.1.2.3 haad lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
618 1.1.2.3 haad
619 1.1.2.3 haad strncpy(lp->d_typename, "lvm", sizeof(lp->d_typename));
620 1.1.2.3 haad lp->d_type = DTYPE_DM;
621 1.1.2.3 haad strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
622 1.1.2.3 haad lp->d_rpm = 3600;
623 1.1.2.3 haad lp->d_interleave = 1;
624 1.1.2.3 haad lp->d_flags = 0;
625 1.1.2.1 haad
626 1.1.2.11 haad pp = &lp->d_partitions[1];
627 1.1.2.3 haad /*
628 1.1.2.3 haad * This is logical offset and therefore it can be 0
629 1.1.2.3 haad * I will consider table offsets later in dmstrategy.
630 1.1.2.3 haad */
631 1.1.2.3 haad pp->p_offset = 0;
632 1.1.2.11 haad pp->p_size = dmp_size * DEV_BSIZE;
633 1.1.2.3 haad pp->p_fstype = FS_BSDFFS; /* default value */
634 1.1.2.3 haad lp->d_npartitions = RAW_PART + 1;
635 1.1.2.3 haad
636 1.1.2.3 haad lp->d_magic = DISKMAGIC;
637 1.1.2.3 haad lp->d_magic2 = DISKMAGIC;
638 1.1.2.3 haad lp->d_checksum = dkcksum(lp);
639 1.1.2.1 haad }
640