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