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