device-mapper.c revision 1.59 1 /* $NetBSD: device-mapper.c,v 1.59 2019/12/22 13:16:09 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
435 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
436 return ENODEV;
437
438 aprint_debug("DIOCGWEDGEINFO ioctl called\n");
439
440 strlcpy(dkw->dkw_devname, dmv->name, 16);
441 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
442 strlcpy(dkw->dkw_parent, dmv->name, 16);
443
444 dkw->dkw_offset = 0;
445 dm_table_disksize(&dmv->table_head, &dkw->dkw_size, NULL);
446 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
447
448 dm_dev_unbusy(dmv);
449 break;
450 }
451 case DIOCGDISKINFO:
452 {
453 struct plistref *pref = (struct plistref *) data;
454
455 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
456 return ENODEV;
457
458 aprint_debug("DIOCGDISKINFO ioctl called\n");
459
460 if (dmv->diskp->dk_info == NULL) {
461 dm_dev_unbusy(dmv);
462 return ENOTSUP;
463 } else
464 prop_dictionary_copyout_ioctl(pref, cmd,
465 dmv->diskp->dk_info);
466
467 dm_dev_unbusy(dmv);
468 break;
469 }
470 case DIOCCACHESYNC:
471 {
472 dm_table_entry_t *table_en;
473 dm_table_t *tbl;
474
475 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
476 return ENODEV;
477
478 aprint_debug("DIOCCACHESYNC ioctl called\n");
479
480 /* Select active table */
481 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
482
483 /*
484 * Call sync target routine for all table entries. Target sync
485 * routine basically call DIOCCACHESYNC on underlying devices.
486 */
487 SLIST_FOREACH(table_en, tbl, next)
488 if (table_en->target->sync)
489 table_en->target->sync(table_en);
490 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
491 dm_dev_unbusy(dmv);
492 break;
493 }
494 case DIOCGSECTORSIZE:
495 {
496 unsigned int secsize, *valp = data;
497
498 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
499 return ENODEV;
500
501 aprint_debug("DIOCGSECTORSIZE ioctl called\n");
502
503 dm_table_disksize(&dmv->table_head, NULL, &secsize);
504 *valp = secsize;
505
506 dm_dev_unbusy(dmv);
507 break;
508 }
509 case DIOCGMEDIASIZE:
510 {
511 off_t *valp = data;
512 uint64_t numsec;
513
514 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
515 return ENODEV;
516
517 aprint_debug("DIOCGMEDIASIZE ioctl called\n");
518
519 dm_table_disksize(&dmv->table_head, &numsec, NULL);
520 *valp = numsec;
521
522 dm_dev_unbusy(dmv);
523 break;
524 }
525 default:
526 aprint_debug("unknown disk_ioctl called\n");
527 return ENOTTY;
528 break; /* NOT REACHED */
529 }
530
531 return 0;
532 }
533
534 /*
535 * Do all IO operations on dm logical devices.
536 */
537 static void
538 dmstrategy(struct buf *bp)
539 {
540 dm_dev_t *dmv;
541 dm_table_t *tbl;
542 dm_table_entry_t *table_en;
543 struct buf *nestbuf;
544
545 uint64_t buf_start, buf_len, issued_len;
546 uint64_t table_start, table_end;
547 uint64_t start, end;
548
549 buf_start = bp->b_blkno * DEV_BSIZE;
550 buf_len = bp->b_bcount;
551
552 table_end = 0;
553 issued_len = 0;
554
555 if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
556 bp->b_error = EIO;
557 bp->b_resid = bp->b_bcount;
558 biodone(bp);
559 return;
560 }
561
562 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
563 dm_table_size(&dmv->table_head)) <= 0) {
564 dm_dev_unbusy(dmv);
565 bp->b_resid = bp->b_bcount;
566 biodone(bp);
567 return;
568 }
569
570 /*
571 * disk(9) is part of device structure and it can't be used without
572 * mutual exclusion, use diskp_mtx until it will be fixed.
573 */
574 mutex_enter(&dmv->diskp_mtx);
575 disk_busy(dmv->diskp);
576 mutex_exit(&dmv->diskp_mtx);
577
578 /* Select active table */
579 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
580
581 /* Nested buffers count down to zero therefore I have
582 to set bp->b_resid to maximal value. */
583 bp->b_resid = bp->b_bcount;
584
585 /*
586 * Find out what tables I want to select.
587 */
588 SLIST_FOREACH(table_en, tbl, next) {
589 /* I need number of bytes not blocks. */
590 table_start = table_en->start * DEV_BSIZE;
591 /*
592 * I have to sub 1 from table_en->length to prevent
593 * off by one error
594 */
595 table_end = table_start + table_en->length * DEV_BSIZE;
596 start = MAX(table_start, buf_start);
597 end = MIN(table_end, buf_start + buf_len);
598
599 aprint_debug("----------------------------------------\n");
600 aprint_debug("table_start %010" PRIu64", table_end %010"
601 PRIu64 "\n", table_start, table_end);
602 aprint_debug("buf_start %010" PRIu64", buf_len %010"
603 PRIu64"\n", buf_start, buf_len);
604 aprint_debug("start-buf_start %010"PRIu64", end %010"
605 PRIu64"\n", start - buf_start, end);
606 aprint_debug("start %010" PRIu64", end %010"
607 PRIu64"\n", start, end);
608 aprint_debug("----------------------------------------\n");
609
610 if (start < end) {
611 /* create nested buffer */
612 nestbuf = getiobuf(NULL, true);
613 nestiobuf_setup(bp, nestbuf, start - buf_start,
614 end - start);
615 issued_len += end - start;
616 /* I need number of blocks. */
617 nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
618 table_en->target->strategy(table_en, nestbuf);
619 }
620 }
621
622 if (issued_len < buf_len)
623 nestiobuf_done(bp, buf_len - issued_len, EINVAL);
624
625 mutex_enter(&dmv->diskp_mtx);
626 disk_unbusy(dmv->diskp, buf_len, bp ? (bp->b_flags & B_READ) : 0);
627 mutex_exit(&dmv->diskp_mtx);
628
629 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
630 dm_dev_unbusy(dmv);
631 }
632
633
634 static int
635 dmread(dev_t dev, struct uio *uio, int flag)
636 {
637
638 return physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio);
639 }
640
641 static int
642 dmwrite(dev_t dev, struct uio *uio, int flag)
643 {
644
645 return physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio);
646 }
647
648 static int
649 dmsize(dev_t dev)
650 {
651 dm_dev_t *dmv;
652 uint64_t size;
653
654 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
655 return -ENOENT;
656
657 size = dm_table_size(&dmv->table_head);
658 dm_dev_unbusy(dmv);
659
660 return size;
661 }
662
663 static void
664 dmminphys(struct buf *bp)
665 {
666
667 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
668 }
669
670 void
671 dmgetproperties(struct disk *disk, dm_table_head_t *head)
672 {
673 uint64_t numsec;
674 unsigned int secsize;
675 struct disk_geom *dg;
676
677 dm_table_disksize(head, &numsec, &secsize);
678
679 dg = &disk->dk_geom;
680
681 memset(dg, 0, sizeof(*dg));
682 dg->dg_secperunit = numsec;
683 dg->dg_secsize = secsize;
684 dg->dg_nsectors = 32;
685 dg->dg_ntracks = 64;
686
687 disk_set_info(NULL, disk, "ESDI");
688 }
689
690 /*
691 * Transform char s to uint64_t offset number.
692 */
693 uint64_t
694 atoi64(const char *s)
695 {
696 uint64_t n;
697 n = 0;
698
699 while (*s != '\0') {
700 if (!isdigit(*s))
701 break;
702
703 n = (10 * n) + (*s - '0');
704 s++;
705 }
706
707 return n;
708 }
709