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