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