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