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