device-mapper.c revision 1.38.2.1 1 1.38.2.1 pgoyette /* $NetBSD: device-mapper.c,v 1.38.2.1 2016/07/18 03:49:59 pgoyette Exp $ */
2 1.2 haad
3 1.2 haad /*
4 1.17 haad * Copyright (c) 2010 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 * I want to say thank you to all people who helped me with this project.
34 1.2 haad */
35 1.2 haad
36 1.2 haad #include <sys/types.h>
37 1.2 haad #include <sys/param.h>
38 1.2 haad
39 1.2 haad #include <sys/buf.h>
40 1.2 haad #include <sys/conf.h>
41 1.12 haad #include <sys/device.h>
42 1.2 haad #include <sys/dkio.h>
43 1.2 haad #include <sys/disk.h>
44 1.2 haad #include <sys/disklabel.h>
45 1.2 haad #include <sys/ioctl.h>
46 1.2 haad #include <sys/ioccom.h>
47 1.2 haad #include <sys/kmem.h>
48 1.28 christos #include <sys/kauth.h>
49 1.38.2.1 pgoyette #include <sys/localcount.h>
50 1.2 haad
51 1.2 haad #include "netbsd-dm.h"
52 1.2 haad #include "dm.h"
53 1.37 christos #include "ioconf.h"
54 1.2 haad
55 1.2 haad static dev_type_open(dmopen);
56 1.2 haad static dev_type_close(dmclose);
57 1.2 haad static dev_type_read(dmread);
58 1.2 haad static dev_type_write(dmwrite);
59 1.2 haad static dev_type_ioctl(dmioctl);
60 1.2 haad static dev_type_strategy(dmstrategy);
61 1.2 haad static dev_type_size(dmsize);
62 1.2 haad
63 1.2 haad /* attach and detach routines */
64 1.22 jakllsch #ifdef _MODULE
65 1.21 jakllsch static int dmdestroy(void);
66 1.22 jakllsch #endif
67 1.2 haad
68 1.21 jakllsch static void dm_doinit(void);
69 1.19 jakllsch
70 1.2 haad static int dm_cmd_to_fun(prop_dictionary_t);
71 1.2 haad static int disk_ioctl_switch(dev_t, u_long, void *);
72 1.2 haad static int dm_ioctl_switch(u_long);
73 1.2 haad static void dmminphys(struct buf *);
74 1.2 haad
75 1.12 haad /* CF attach/detach functions used for power management */
76 1.12 haad static int dm_detach(device_t, int);
77 1.12 haad static void dm_attach(device_t, device_t, void *);
78 1.12 haad static int dm_match(device_t, cfdata_t, void *);
79 1.12 haad
80 1.2 haad /* ***Variable-definitions*** */
81 1.38.2.1 pgoyette
82 1.38.2.1 pgoyette #ifdef _MODULE
83 1.38.2.1 pgoyette struct localcount dm_b_localcount, dm_c_localcount;
84 1.38.2.1 pgoyette #endif
85 1.38.2.1 pgoyette
86 1.2 haad const struct bdevsw dm_bdevsw = {
87 1.6 haad .d_open = dmopen,
88 1.6 haad .d_close = dmclose,
89 1.6 haad .d_strategy = dmstrategy,
90 1.6 haad .d_ioctl = dmioctl,
91 1.6 haad .d_dump = nodump,
92 1.6 haad .d_psize = dmsize,
93 1.33 dholland .d_discard = nodiscard,
94 1.38.2.1 pgoyette #ifdef _MODULE
95 1.38.2.1 pgoyette .d_localcount = &dm_b_localcount,
96 1.38.2.1 pgoyette #endif
97 1.6 haad .d_flag = D_DISK | D_MPSAFE
98 1.2 haad };
99 1.2 haad
100 1.2 haad const struct cdevsw dm_cdevsw = {
101 1.6 haad .d_open = dmopen,
102 1.6 haad .d_close = dmclose,
103 1.6 haad .d_read = dmread,
104 1.6 haad .d_write = dmwrite,
105 1.6 haad .d_ioctl = dmioctl,
106 1.6 haad .d_stop = nostop,
107 1.6 haad .d_tty = notty,
108 1.6 haad .d_poll = nopoll,
109 1.6 haad .d_mmap = nommap,
110 1.6 haad .d_kqfilter = nokqfilter,
111 1.34 dholland .d_discard = nodiscard,
112 1.38.2.1 pgoyette #ifdef _MODULE
113 1.38.2.1 pgoyette .d_localcount = &dm_c_localcount,
114 1.38.2.1 pgoyette #endif
115 1.6 haad .d_flag = D_DISK | D_MPSAFE
116 1.6 haad };
117 1.6 haad
118 1.6 haad const struct dkdriver dmdkdriver = {
119 1.6 haad .d_strategy = dmstrategy
120 1.2 haad };
121 1.2 haad
122 1.12 haad CFATTACH_DECL3_NEW(dm, 0,
123 1.12 haad dm_match, dm_attach, dm_detach, NULL, NULL, NULL,
124 1.12 haad DVF_DETACH_SHUTDOWN);
125 1.12 haad
126 1.12 haad extern struct cfdriver dm_cd;
127 1.12 haad
128 1.26 haad extern uint32_t dm_dev_counter;
129 1.2 haad
130 1.2 haad /*
131 1.2 haad * This array is used to translate cmd to function pointer.
132 1.2 haad *
133 1.38 msaitoh * Interface between libdevmapper and lvm2tools uses different
134 1.2 haad * names for one IOCTL call because libdevmapper do another thing
135 1.2 haad * then. When I run "info" or "mknodes" libdevmapper will send same
136 1.2 haad * ioctl to kernel but will do another things in userspace.
137 1.2 haad *
138 1.2 haad */
139 1.28 christos static const struct cmd_function cmd_fn[] = {
140 1.28 christos { .cmd = "version", .fn = dm_get_version_ioctl, .allowed = 1 },
141 1.28 christos { .cmd = "targets", .fn = dm_list_versions_ioctl, .allowed = 1 },
142 1.28 christos { .cmd = "create", .fn = dm_dev_create_ioctl, .allowed = 0 },
143 1.28 christos { .cmd = "info", .fn = dm_dev_status_ioctl, .allowed = 1 },
144 1.28 christos { .cmd = "mknodes", .fn = dm_dev_status_ioctl, .allowed = 1 },
145 1.28 christos { .cmd = "names", .fn = dm_dev_list_ioctl, .allowed = 1 },
146 1.28 christos { .cmd = "suspend", .fn = dm_dev_suspend_ioctl, .allowed = 0 },
147 1.38 msaitoh { .cmd = "remove", .fn = dm_dev_remove_ioctl, .allowed = 0 },
148 1.28 christos { .cmd = "rename", .fn = dm_dev_rename_ioctl, .allowed = 0 },
149 1.28 christos { .cmd = "resume", .fn = dm_dev_resume_ioctl, .allowed = 0 },
150 1.28 christos { .cmd = "clear", .fn = dm_table_clear_ioctl, .allowed = 0 },
151 1.28 christos { .cmd = "deps", .fn = dm_table_deps_ioctl, .allowed = 1 },
152 1.28 christos { .cmd = "reload", .fn = dm_table_load_ioctl, .allowed = 0 },
153 1.28 christos { .cmd = "status", .fn = dm_table_status_ioctl, .allowed = 1 },
154 1.28 christos { .cmd = "table", .fn = dm_table_status_ioctl, .allowed = 1 },
155 1.38 msaitoh { .cmd = NULL, .fn = NULL, .allowed = 0 }
156 1.2 haad };
157 1.2 haad
158 1.21 jakllsch #ifdef _MODULE
159 1.21 jakllsch #include <sys/module.h>
160 1.21 jakllsch
161 1.21 jakllsch /* Autoconf defines */
162 1.21 jakllsch CFDRIVER_DECL(dm, DV_DISK, NULL);
163 1.21 jakllsch
164 1.32 pgoyette MODULE(MODULE_CLASS_DRIVER, dm, "dk_subr");
165 1.2 haad
166 1.2 haad /* New module handle routine */
167 1.2 haad static int
168 1.2 haad dm_modcmd(modcmd_t cmd, void *arg)
169 1.2 haad {
170 1.23 haad #ifdef _MODULE
171 1.35 justin int error;
172 1.35 justin devmajor_t bmajor, cmajor;
173 1.12 haad
174 1.12 haad error = 0;
175 1.16 jakllsch bmajor = -1;
176 1.16 jakllsch cmajor = -1;
177 1.2 haad
178 1.2 haad switch (cmd) {
179 1.2 haad case MODULE_CMD_INIT:
180 1.12 haad error = config_cfdriver_attach(&dm_cd);
181 1.12 haad if (error)
182 1.12 haad break;
183 1.12 haad
184 1.21 jakllsch error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
185 1.12 haad if (error) {
186 1.21 jakllsch aprint_error("%s: unable to register cfattach\n",
187 1.21 jakllsch dm_cd.cd_name);
188 1.21 jakllsch return error;
189 1.16 jakllsch }
190 1.12 haad
191 1.16 jakllsch error = devsw_attach(dm_cd.cd_name, &dm_bdevsw, &bmajor,
192 1.16 jakllsch &dm_cdevsw, &cmajor);
193 1.24 haad if (error == EEXIST)
194 1.24 haad error = 0;
195 1.16 jakllsch if (error) {
196 1.16 jakllsch config_cfattach_detach(dm_cd.cd_name, &dm_ca);
197 1.16 jakllsch config_cfdriver_detach(&dm_cd);
198 1.12 haad break;
199 1.12 haad }
200 1.21 jakllsch
201 1.21 jakllsch dm_doinit();
202 1.21 jakllsch
203 1.2 haad break;
204 1.2 haad
205 1.2 haad case MODULE_CMD_FINI:
206 1.4 haad /*
207 1.4 haad * Disable unloading of dm module if there are any devices
208 1.4 haad * defined in driver. This is probably too strong we need
209 1.4 haad * to disable auto-unload only if there is mounted dm device
210 1.4 haad * present.
211 1.38 msaitoh */
212 1.16 jakllsch if (dm_dev_counter > 0)
213 1.4 haad return EBUSY;
214 1.12 haad
215 1.17 haad error = dmdestroy();
216 1.12 haad if (error)
217 1.12 haad break;
218 1.12 haad
219 1.12 haad config_cfdriver_detach(&dm_cd);
220 1.12 haad
221 1.12 haad devsw_detach(&dm_bdevsw, &dm_cdevsw);
222 1.2 haad break;
223 1.2 haad case MODULE_CMD_STAT:
224 1.2 haad return ENOTTY;
225 1.2 haad
226 1.2 haad default:
227 1.2 haad return ENOTTY;
228 1.2 haad }
229 1.2 haad
230 1.12 haad return error;
231 1.23 haad #else
232 1.23 haad return ENOTTY;
233 1.23 haad #endif
234 1.2 haad }
235 1.21 jakllsch #endif /* _MODULE */
236 1.2 haad
237 1.12 haad /*
238 1.12 haad * dm_match:
239 1.12 haad *
240 1.12 haad * Autoconfiguration match function for pseudo-device glue.
241 1.12 haad */
242 1.12 haad static int
243 1.38 msaitoh dm_match(device_t parent, cfdata_t match, void *aux)
244 1.12 haad {
245 1.12 haad
246 1.12 haad /* Pseudo-device; always present. */
247 1.12 haad return (1);
248 1.12 haad }
249 1.12 haad
250 1.12 haad /*
251 1.12 haad * dm_attach:
252 1.12 haad *
253 1.12 haad * Autoconfiguration attach function for pseudo-device glue.
254 1.12 haad */
255 1.12 haad static void
256 1.38 msaitoh dm_attach(device_t parent, device_t self, void *aux)
257 1.12 haad {
258 1.12 haad return;
259 1.12 haad }
260 1.12 haad
261 1.12 haad
262 1.12 haad /*
263 1.12 haad * dm_detach:
264 1.12 haad *
265 1.12 haad * Autoconfiguration detach function for pseudo-device glue.
266 1.12 haad * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
267 1.38 msaitoh * remove devices created in device-mapper.
268 1.12 haad */
269 1.12 haad static int
270 1.12 haad dm_detach(device_t self, int flags)
271 1.12 haad {
272 1.12 haad dm_dev_t *dmv;
273 1.12 haad
274 1.12 haad /* Detach device from global device list */
275 1.12 haad if ((dmv = dm_dev_detach(self)) == NULL)
276 1.12 haad return ENOENT;
277 1.12 haad
278 1.12 haad /* Destroy active table first. */
279 1.12 haad dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
280 1.12 haad
281 1.12 haad /* Destroy inactive table if exits, too. */
282 1.12 haad dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
283 1.38 msaitoh
284 1.12 haad dm_table_head_destroy(&dmv->table_head);
285 1.12 haad
286 1.12 haad /* Destroy disk device structure */
287 1.12 haad disk_detach(dmv->diskp);
288 1.12 haad disk_destroy(dmv->diskp);
289 1.12 haad
290 1.12 haad /* Destroy device */
291 1.12 haad (void)dm_dev_free(dmv);
292 1.12 haad
293 1.12 haad /* Decrement device counter After removing device */
294 1.26 haad atomic_dec_32(&dm_dev_counter);
295 1.12 haad
296 1.12 haad return 0;
297 1.12 haad }
298 1.12 haad
299 1.21 jakllsch static void
300 1.21 jakllsch dm_doinit(void)
301 1.2 haad {
302 1.17 haad dm_target_init();
303 1.17 haad dm_dev_init();
304 1.17 haad dm_pdev_init();
305 1.17 haad }
306 1.17 haad
307 1.17 haad /* attach routine */
308 1.18 jakllsch void
309 1.18 jakllsch dmattach(int n)
310 1.17 haad {
311 1.21 jakllsch int error;
312 1.21 jakllsch
313 1.21 jakllsch error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
314 1.21 jakllsch if (error) {
315 1.21 jakllsch aprint_error("%s: unable to register cfattach\n",
316 1.21 jakllsch dm_cd.cd_name);
317 1.21 jakllsch } else {
318 1.21 jakllsch dm_doinit();
319 1.21 jakllsch }
320 1.2 haad }
321 1.2 haad
322 1.22 jakllsch #ifdef _MODULE
323 1.2 haad /* Destroy routine */
324 1.21 jakllsch static int
325 1.2 haad dmdestroy(void)
326 1.2 haad {
327 1.17 haad int error;
328 1.12 haad
329 1.17 haad error = config_cfattach_detach(dm_cd.cd_name, &dm_ca);
330 1.17 haad if (error)
331 1.17 haad return error;
332 1.38 msaitoh
333 1.2 haad dm_dev_destroy();
334 1.2 haad dm_pdev_destroy();
335 1.2 haad dm_target_destroy();
336 1.2 haad
337 1.17 haad return 0;
338 1.2 haad }
339 1.22 jakllsch #endif /* _MODULE */
340 1.2 haad
341 1.2 haad static int
342 1.2 haad dmopen(dev_t dev, int flags, int mode, struct lwp *l)
343 1.2 haad {
344 1.12 haad
345 1.13 haad aprint_debug("dm open routine called %" PRIu32 "\n", minor(dev));
346 1.2 haad return 0;
347 1.2 haad }
348 1.2 haad
349 1.2 haad static int
350 1.2 haad dmclose(dev_t dev, int flags, int mode, struct lwp *l)
351 1.2 haad {
352 1.12 haad
353 1.13 haad aprint_debug("dm close routine called %" PRIu32 "\n", minor(dev));
354 1.2 haad return 0;
355 1.2 haad }
356 1.2 haad
357 1.2 haad
358 1.2 haad static int
359 1.2 haad dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
360 1.2 haad {
361 1.2 haad int r;
362 1.2 haad prop_dictionary_t dm_dict_in;
363 1.2 haad
364 1.2 haad r = 0;
365 1.2 haad
366 1.2 haad aprint_debug("dmioctl called\n");
367 1.2 haad KASSERT(data != NULL);
368 1.38 msaitoh
369 1.13 haad if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
370 1.2 haad struct plistref *pref = (struct plistref *) data;
371 1.2 haad
372 1.13 haad /* Check if we were called with NETBSD_DM_IOCTL ioctl
373 1.13 haad otherwise quit. */
374 1.13 haad if ((r = dm_ioctl_switch(cmd)) != 0)
375 1.13 haad return r;
376 1.13 haad
377 1.38 msaitoh if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in))
378 1.38 msaitoh != 0)
379 1.2 haad return r;
380 1.2 haad
381 1.14 haad if ((r = dm_check_version(dm_dict_in)) != 0)
382 1.14 haad goto cleanup_exit;
383 1.2 haad
384 1.2 haad /* run ioctl routine */
385 1.14 haad if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
386 1.14 haad goto cleanup_exit;
387 1.13 haad
388 1.14 haad cleanup_exit:
389 1.2 haad r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
390 1.2 haad prop_object_release(dm_dict_in);
391 1.2 haad }
392 1.2 haad
393 1.2 haad return r;
394 1.2 haad }
395 1.2 haad
396 1.2 haad /*
397 1.2 haad * Translate command sent from libdevmapper to func.
398 1.2 haad */
399 1.2 haad static int
400 1.38 msaitoh dm_cmd_to_fun(prop_dictionary_t dm_dict)
401 1.38 msaitoh {
402 1.2 haad int i, r;
403 1.2 haad prop_string_t command;
404 1.38 msaitoh
405 1.2 haad r = 0;
406 1.13 haad
407 1.2 haad if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
408 1.2 haad return EINVAL;
409 1.13 haad
410 1.2 haad for(i = 0; cmd_fn[i].cmd != NULL; i++)
411 1.2 haad if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
412 1.2 haad break;
413 1.2 haad
414 1.38 msaitoh if (!cmd_fn[i].allowed &&
415 1.29 elad (r = kauth_authorize_system(kauth_cred_get(),
416 1.29 elad KAUTH_SYSTEM_DEVMAPPER, 0, NULL, NULL, NULL)) != 0)
417 1.28 christos return r;
418 1.28 christos
419 1.2 haad if (cmd_fn[i].cmd == NULL)
420 1.2 haad return EINVAL;
421 1.2 haad
422 1.2 haad aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
423 1.2 haad r = cmd_fn[i].fn(dm_dict);
424 1.13 haad
425 1.2 haad return r;
426 1.2 haad }
427 1.2 haad
428 1.2 haad /* Call apropriate ioctl handler function. */
429 1.2 haad static int
430 1.2 haad dm_ioctl_switch(u_long cmd)
431 1.2 haad {
432 1.2 haad
433 1.2 haad switch(cmd) {
434 1.13 haad
435 1.2 haad case NETBSD_DM_IOCTL:
436 1.13 haad aprint_debug("dm NetBSD_DM_IOCTL called\n");
437 1.2 haad break;
438 1.2 haad default:
439 1.13 haad aprint_debug("dm unknown ioctl called\n");
440 1.2 haad return ENOTTY;
441 1.2 haad break; /* NOT REACHED */
442 1.2 haad }
443 1.2 haad
444 1.13 haad return 0;
445 1.2 haad }
446 1.2 haad
447 1.2 haad /*
448 1.2 haad * Check for disk specific ioctls.
449 1.2 haad */
450 1.2 haad
451 1.2 haad static int
452 1.2 haad disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
453 1.2 haad {
454 1.2 haad dm_dev_t *dmv;
455 1.13 haad
456 1.20 haad /* disk ioctls make sense only on block devices */
457 1.20 haad if (minor(dev) == 0)
458 1.20 haad return ENOTTY;
459 1.38 msaitoh
460 1.2 haad switch(cmd) {
461 1.2 haad case DIOCGWEDGEINFO:
462 1.2 haad {
463 1.2 haad struct dkwedge_info *dkw = (void *) data;
464 1.27 mlelstv unsigned secsize;
465 1.2 haad
466 1.2 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
467 1.13 haad return ENODEV;
468 1.13 haad
469 1.9 joerg aprint_debug("DIOCGWEDGEINFO ioctl called\n");
470 1.13 haad
471 1.2 haad strlcpy(dkw->dkw_devname, dmv->name, 16);
472 1.2 haad strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
473 1.2 haad strlcpy(dkw->dkw_parent, dmv->name, 16);
474 1.13 haad
475 1.2 haad dkw->dkw_offset = 0;
476 1.27 mlelstv dm_table_disksize(&dmv->table_head, &dkw->dkw_size, &secsize);
477 1.2 haad strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
478 1.2 haad
479 1.2 haad dm_dev_unbusy(dmv);
480 1.2 haad break;
481 1.2 haad }
482 1.7 haad
483 1.7 haad case DIOCGDISKINFO:
484 1.2 haad {
485 1.7 haad struct plistref *pref = (struct plistref *) data;
486 1.2 haad
487 1.2 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
488 1.13 haad return ENODEV;
489 1.13 haad
490 1.7 haad if (dmv->diskp->dk_info == NULL) {
491 1.7 haad dm_dev_unbusy(dmv);
492 1.7 haad return ENOTSUP;
493 1.7 haad } else
494 1.7 haad prop_dictionary_copyout_ioctl(pref, cmd,
495 1.7 haad dmv->diskp->dk_info);
496 1.2 haad
497 1.2 haad dm_dev_unbusy(dmv);
498 1.2 haad break;
499 1.2 haad }
500 1.23 haad
501 1.23 haad case DIOCCACHESYNC:
502 1.23 haad {
503 1.23 haad dm_table_entry_t *table_en;
504 1.23 haad dm_table_t *tbl;
505 1.38 msaitoh
506 1.23 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
507 1.23 haad return ENODEV;
508 1.23 haad
509 1.23 haad /* Select active table */
510 1.23 haad tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
511 1.23 haad
512 1.23 haad /*
513 1.23 haad * Call sync target routine for all table entries. Target sync
514 1.23 haad * routine basically call DIOCCACHESYNC on underlying devices.
515 1.23 haad */
516 1.23 haad SLIST_FOREACH(table_en, tbl, next)
517 1.23 haad {
518 1.31 christos (void)table_en->target->sync(table_en);
519 1.23 haad }
520 1.23 haad dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
521 1.23 haad dm_dev_unbusy(dmv);
522 1.23 haad break;
523 1.23 haad }
524 1.38 msaitoh
525 1.38 msaitoh
526 1.2 haad default:
527 1.2 haad aprint_debug("unknown disk_ioctl called\n");
528 1.13 haad return ENOTTY;
529 1.2 haad break; /* NOT REACHED */
530 1.2 haad }
531 1.13 haad
532 1.2 haad return 0;
533 1.2 haad }
534 1.2 haad
535 1.2 haad /*
536 1.2 haad * Do all IO operations on dm logical devices.
537 1.2 haad */
538 1.2 haad static void
539 1.2 haad dmstrategy(struct buf *bp)
540 1.2 haad {
541 1.2 haad dm_dev_t *dmv;
542 1.2 haad dm_table_t *tbl;
543 1.2 haad dm_table_entry_t *table_en;
544 1.2 haad struct buf *nestbuf;
545 1.2 haad
546 1.2 haad uint64_t buf_start, buf_len, issued_len;
547 1.2 haad uint64_t table_start, table_end;
548 1.2 haad uint64_t start, end;
549 1.13 haad
550 1.2 haad buf_start = bp->b_blkno * DEV_BSIZE;
551 1.2 haad buf_len = bp->b_bcount;
552 1.2 haad
553 1.38 msaitoh tbl = NULL;
554 1.2 haad
555 1.2 haad table_end = 0;
556 1.2 haad issued_len = 0;
557 1.2 haad
558 1.2 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
559 1.2 haad bp->b_error = EIO;
560 1.2 haad bp->b_resid = bp->b_bcount;
561 1.2 haad biodone(bp);
562 1.2 haad return;
563 1.38 msaitoh }
564 1.6 haad
565 1.8 jakllsch if (bounds_check_with_mediasize(bp, DEV_BSIZE,
566 1.8 jakllsch dm_table_size(&dmv->table_head)) <= 0) {
567 1.8 jakllsch dm_dev_unbusy(dmv);
568 1.8 jakllsch bp->b_resid = bp->b_bcount;
569 1.8 jakllsch biodone(bp);
570 1.8 jakllsch return;
571 1.8 jakllsch }
572 1.8 jakllsch
573 1.11 haad /*
574 1.11 haad * disk(9) is part of device structure and it can't be used without
575 1.11 haad * mutual exclusion, use diskp_mtx until it will be fixed.
576 1.11 haad */
577 1.11 haad mutex_enter(&dmv->diskp_mtx);
578 1.6 haad disk_busy(dmv->diskp);
579 1.11 haad mutex_exit(&dmv->diskp_mtx);
580 1.13 haad
581 1.2 haad /* Select active table */
582 1.2 haad tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
583 1.2 haad
584 1.2 haad /* Nested buffers count down to zero therefore I have
585 1.2 haad to set bp->b_resid to maximal value. */
586 1.2 haad bp->b_resid = bp->b_bcount;
587 1.13 haad
588 1.2 haad /*
589 1.2 haad * Find out what tables I want to select.
590 1.2 haad */
591 1.2 haad SLIST_FOREACH(table_en, tbl, next)
592 1.2 haad {
593 1.2 haad /* I need need number of bytes not blocks. */
594 1.2 haad table_start = table_en->start * DEV_BSIZE;
595 1.2 haad /*
596 1.2 haad * I have to sub 1 from table_en->length to prevent
597 1.2 haad * off by one error
598 1.2 haad */
599 1.2 haad table_end = table_start + (table_en->length)* DEV_BSIZE;
600 1.2 haad
601 1.2 haad start = MAX(table_start, buf_start);
602 1.2 haad
603 1.2 haad end = MIN(table_end, buf_start + buf_len);
604 1.2 haad
605 1.2 haad aprint_debug("----------------------------------------\n");
606 1.2 haad aprint_debug("table_start %010" PRIu64", table_end %010"
607 1.2 haad PRIu64 "\n", table_start, table_end);
608 1.2 haad aprint_debug("buf_start %010" PRIu64", buf_len %010"
609 1.2 haad PRIu64"\n", buf_start, buf_len);
610 1.2 haad aprint_debug("start-buf_start %010"PRIu64", end %010"
611 1.2 haad PRIu64"\n", start - buf_start, end);
612 1.2 haad aprint_debug("start %010" PRIu64" , end %010"
613 1.2 haad PRIu64"\n", start, end);
614 1.2 haad aprint_debug("\n----------------------------------------\n");
615 1.2 haad
616 1.2 haad if (start < end) {
617 1.2 haad /* create nested buffer */
618 1.2 haad nestbuf = getiobuf(NULL, true);
619 1.2 haad
620 1.2 haad nestiobuf_setup(bp, nestbuf, start - buf_start,
621 1.2 haad (end - start));
622 1.2 haad
623 1.2 haad issued_len += end - start;
624 1.13 haad
625 1.2 haad /* I need number of blocks. */
626 1.2 haad nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
627 1.2 haad
628 1.2 haad table_en->target->strategy(table_en, nestbuf);
629 1.2 haad }
630 1.8 jakllsch }
631 1.2 haad
632 1.2 haad if (issued_len < buf_len)
633 1.2 haad nestiobuf_done(bp, buf_len - issued_len, EINVAL);
634 1.2 haad
635 1.11 haad mutex_enter(&dmv->diskp_mtx);
636 1.6 haad disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0);
637 1.11 haad mutex_exit(&dmv->diskp_mtx);
638 1.13 haad
639 1.2 haad dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
640 1.2 haad dm_dev_unbusy(dmv);
641 1.2 haad
642 1.2 haad return;
643 1.2 haad }
644 1.2 haad
645 1.2 haad
646 1.2 haad static int
647 1.2 haad dmread(dev_t dev, struct uio *uio, int flag)
648 1.2 haad {
649 1.13 haad
650 1.2 haad return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
651 1.2 haad }
652 1.2 haad
653 1.2 haad static int
654 1.2 haad dmwrite(dev_t dev, struct uio *uio, int flag)
655 1.2 haad {
656 1.13 haad
657 1.2 haad return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
658 1.2 haad }
659 1.2 haad
660 1.2 haad static int
661 1.2 haad dmsize(dev_t dev)
662 1.2 haad {
663 1.2 haad dm_dev_t *dmv;
664 1.2 haad uint64_t size;
665 1.13 haad
666 1.2 haad size = 0;
667 1.13 haad
668 1.2 haad if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
669 1.2 haad return -ENOENT;
670 1.13 haad
671 1.2 haad size = dm_table_size(&dmv->table_head);
672 1.2 haad dm_dev_unbusy(dmv);
673 1.38 msaitoh
674 1.2 haad return size;
675 1.2 haad }
676 1.2 haad
677 1.2 haad static void
678 1.2 haad dmminphys(struct buf *bp)
679 1.2 haad {
680 1.13 haad
681 1.2 haad bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
682 1.2 haad }
683 1.2 haad
684 1.2 haad void
685 1.7 haad dmgetproperties(struct disk *disk, dm_table_head_t *head)
686 1.2 haad {
687 1.27 mlelstv uint64_t numsec;
688 1.27 mlelstv unsigned secsize;
689 1.13 haad
690 1.27 mlelstv dm_table_disksize(head, &numsec, &secsize);
691 1.7 haad
692 1.30 christos struct disk_geom *dg = &disk->dk_geom;
693 1.2 haad
694 1.30 christos memset(dg, 0, sizeof(*dg));
695 1.30 christos dg->dg_secperunit = numsec;
696 1.30 christos dg->dg_secsize = secsize;
697 1.30 christos dg->dg_nsectors = 32;
698 1.30 christos dg->dg_ntracks = 64;
699 1.7 haad
700 1.30 christos disk_set_info(NULL, disk, "ESDI");
701 1.27 mlelstv }
702