1 /* $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * I want to say thank you to all people who helped me with this project.
36 #include <sys/types.h>
37 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/devfs.h>
44 #include <sys/disklabel.h>
45 #include <sys/dtype.h>
46 #include <sys/ioccom.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/sysctl.h>
50 #include <dev/disk/dm/dm.h>
52 #include "netbsd-dm.h"
54 static d_ioctl_t dmioctl;
55 static d_open_t dmopen;
56 static d_close_t dmclose;
57 static d_psize_t dmsize;
58 static d_strategy_t dmstrategy;
59 static d_dump_t dmdump;
61 /* attach and detach routines */
63 static int dm_modcmd(module_t mod, int cmd, void *unused);
64 static int dmdestroy(void);
66 static void dm_doinit(void);
68 static int dm_cmd_to_fun(prop_dictionary_t);
69 static int disk_ioctl_switch(cdev_t, u_long, void *);
70 static int dm_ioctl_switch(u_long);
72 static void dmminphys(struct buf *);
75 struct devfs_bitmap dm_minor_bitmap;
77 /* ***Variable-definitions*** */
78 struct dev_ops dm_ops = {
79 { "dm", 0, D_DISK | D_MPSAFE },
85 .d_strategy = dmstrategy,
91 MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations");
93 int dm_debug_level = 0;
95 extern uint64_t dm_dev_counter;
99 static moduledata_t dm_mod = {
104 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
105 MODULE_VERSION(dm, 1);
108 * This array is used to translate cmd to function pointer.
110 * Interface between libdevmapper and lvm2tools uses different
111 * names for one IOCTL call because libdevmapper do another thing
112 * then. When I run "info" or "mknodes" libdevmapper will send same
113 * ioctl to kernel but will do another things in userspace.
116 static struct cmd_function cmd_fn[] = {
117 { .cmd = "version", .fn = dm_get_version_ioctl},
118 { .cmd = "targets", .fn = dm_list_versions_ioctl},
119 { .cmd = "create", .fn = dm_dev_create_ioctl},
120 { .cmd = "info", .fn = dm_dev_status_ioctl},
121 { .cmd = "mknodes", .fn = dm_dev_status_ioctl},
122 { .cmd = "names", .fn = dm_dev_list_ioctl},
123 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
124 { .cmd = "remove", .fn = dm_dev_remove_ioctl},
125 { .cmd = "rename", .fn = dm_dev_rename_ioctl},
126 { .cmd = "resume", .fn = dm_dev_resume_ioctl},
127 { .cmd = "clear", .fn = dm_table_clear_ioctl},
128 { .cmd = "deps", .fn = dm_table_deps_ioctl},
129 { .cmd = "reload", .fn = dm_table_load_ioctl},
130 { .cmd = "status", .fn = dm_table_status_ioctl},
131 { .cmd = "table", .fn = dm_table_status_ioctl},
135 /* New module handle routine */
137 dm_modcmd(module_t mod, int cmd, void *unused)
139 int error, bmajor, cmajor;
147 devfs_clone_bitmap_init(&dm_minor_bitmap);
149 kprintf("Device Mapper version %d.%d.%d loaded\n",
150 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
155 * Disable unloading of dm module if there are any devices
156 * defined in driver. This is probably too strong we need
157 * to disable auto-unload only if there is mounted dm device
160 if (dm_dev_counter > 0)
166 kprintf("Device Mapper unloaded\n");
177 * dm_detach is called to completely destroy & remove a dm disk device.
180 dm_detach(dm_dev_t *dmv)
184 /* Remove device from list and wait for refcnt to drop to zero */
185 dm_dev_rem(dmv, NULL, NULL, -1);
187 /* Destroy active table first. */
188 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
190 /* Destroy inactive table if exits, too. */
191 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
193 dm_table_head_destroy(&dmv->table_head);
195 minor = dkunit(dmv->devt);
196 disk_destroy(dmv->diskp);
197 devstat_remove_entry(&dmv->stats);
199 release_dev(dmv->devt);
200 devfs_clone_bitmap_put(&dm_minor_bitmap, minor);
203 (void)dm_dev_free(dmv);
205 /* Decrement device counter After removing device */
206 --dm_dev_counter; /* XXX: was atomic 64 */
217 dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
220 /* Destroy routine */
234 dmopen(struct dev_open_args *ap)
236 cdev_t dev = ap->a_head.a_dev;
239 /* Shortcut for the control device */
243 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
249 aprint_debug("dm open routine called %" PRIu32 "\n",
250 minor(ap->a_head.a_dev));
255 dmclose(struct dev_close_args *ap)
257 cdev_t dev = ap->a_head.a_dev;
260 /* Shortcut for the control device */
264 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
270 aprint_debug("dm close routine called %" PRIu32 "\n",
271 minor(ap->a_head.a_dev));
277 dmioctl(struct dev_ioctl_args *ap)
279 cdev_t dev = ap->a_head.a_dev;
280 u_long cmd = ap->a_cmd;
281 void *data = ap->a_data;
284 prop_dictionary_t dm_dict_in;
288 aprint_debug("dmioctl called\n");
290 KKASSERT(data != NULL);
292 if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
293 struct plistref *pref = (struct plistref *) data;
295 /* Check if we were called with NETBSD_DM_IOCTL ioctl
297 if ((r = dm_ioctl_switch(cmd)) != 0)
300 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
303 if ((r = dm_check_version(dm_dict_in)) != 0)
306 /* run ioctl routine */
307 if ((err = dm_cmd_to_fun(dm_dict_in)) != 0)
311 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
312 prop_object_release(dm_dict_in);
316 * Return the error of the actual command if one one has
317 * happened. Otherwise return 'r' which indicates errors
318 * that occurred during helper operations.
320 return (err != 0)?err:r;
324 * Translate command sent from libdevmapper to func.
327 dm_cmd_to_fun(prop_dictionary_t dm_dict){
329 prop_string_t command;
333 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
336 for(i = 0; cmd_fn[i].cmd != NULL; i++)
337 if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
340 if (cmd_fn[i].cmd == NULL)
343 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
344 r = cmd_fn[i].fn(dm_dict);
349 /* Call apropriate ioctl handler function. */
351 dm_ioctl_switch(u_long cmd)
356 case NETBSD_DM_IOCTL:
357 aprint_debug("dm NetBSD_DM_IOCTL called\n");
360 aprint_debug("dm unknown ioctl called\n");
362 break; /* NOT REACHED */
369 * Check for disk specific ioctls.
373 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
377 /* disk ioctls make sense only on block devices */
384 struct partinfo *dpart;
386 dpart = (void *)data;
387 bzero(dpart, sizeof(*dpart));
389 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
391 if (dmv->diskp->d_info.d_media_blksize == 0) {
395 size = dm_table_size(&dmv->table_head);
396 dpart->media_offset = 0;
397 dpart->media_size = size * DEV_BSIZE;
398 dpart->media_blocks = size;
399 dpart->media_blksize = DEV_BSIZE;
400 dpart->fstype = FS_BSDFFS;
407 aprint_debug("unknown disk_ioctl called\n");
409 break; /* NOT REACHED */
416 * Do all IO operations on dm logical devices.
419 dmstrategy(struct dev_strategy_args *ap)
421 cdev_t dev = ap->a_head.a_dev;
422 struct bio *bio = ap->a_bio;
423 struct buf *bp = bio->bio_buf;
428 dm_table_entry_t *table_en;
433 uint64_t buf_start, buf_len, issued_len;
434 uint64_t table_start, table_end;
437 buf_start = bio->bio_offset;
438 buf_len = bp->b_bcount;
446 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) {
448 bp->b_resid = bp->b_bcount;
456 case BUF_CMD_FREEBLKS:
461 KKASSERT(buf_len == 0);
466 bp->b_resid = bp->b_bcount;
472 bounds_check_with_mediasize(bio, DEV_BSIZE,
473 dm_table_size(&dmv->table_head)) <= 0) {
475 bp->b_resid = bp->b_bcount;
480 /* Select active table */
481 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
484 devstat_start_transaction(&dmv->stats);
487 * Find out what tables I want to select.
489 SLIST_FOREACH(table_en, tbl, next) {
491 * I need need number of bytes not blocks.
493 table_start = table_en->start * DEV_BSIZE;
494 table_end = table_start + (table_en->length) * DEV_BSIZE;
497 * Calculate the start and end
499 start = MAX(table_start, buf_start);
500 end = MIN(table_end, buf_start + buf_len);
502 aprint_debug("----------------------------------------\n");
503 aprint_debug("table_start %010" PRIu64", table_end %010"
504 PRIu64 "\n", table_start, table_end);
505 aprint_debug("buf_start %010" PRIu64", buf_len %010"
506 PRIu64"\n", buf_start, buf_len);
507 aprint_debug("start-buf_start %010"PRIu64", end %010"
508 PRIu64"\n", start - buf_start, end);
509 aprint_debug("start %010" PRIu64" , end %010"
510 PRIu64"\n", start, end);
511 aprint_debug("\n----------------------------------------\n");
514 nestbuf = getpbuf(NULL);
515 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
517 nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
518 nestbuf->b_bio1.bio_offset = 0;
519 table_en->target->strategy(table_en, nestbuf);
520 } else if (start < end) {
521 nestbuf = getpbuf(NULL);
522 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
524 nestiobuf_add(bio, nestbuf,
525 start - buf_start, (end - start),
527 issued_len += end - start;
529 nestbuf->b_bio1.bio_offset = (start - table_start);
530 table_en->target->strategy(table_en, nestbuf);
534 if (issued_len < buf_len)
535 nestiobuf_error(bio, EINVAL);
536 nestiobuf_start(bio);
537 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
544 dmdump(struct dev_dump_args *ap)
546 cdev_t dev = ap->a_head.a_dev;
549 dm_table_entry_t *table_en;
551 uint64_t buf_start, buf_len, issued_len;
552 uint64_t table_start, table_end;
553 uint64_t start, end, data_offset;
558 buf_start = ap->a_offset;
559 buf_len = ap->a_length;
567 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) {
571 /* Select active table */
572 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
576 * Find out what tables I want to select.
578 SLIST_FOREACH(table_en, tbl, next) {
580 * I need need number of bytes not blocks.
582 table_start = table_en->start * DEV_BSIZE;
583 table_end = table_start + (table_en->length) * DEV_BSIZE;
586 * Calculate the start and end
588 start = MAX(table_start, buf_start);
589 end = MIN(table_end, buf_start + buf_len);
591 if (ap->a_length == 0) {
592 if (table_en->target->dump == NULL) {
597 table_en->target->dump(table_en, NULL, 0, 0);
598 } else if (start < end) {
599 data_offset = start - buf_start;
600 offset = start - table_start;
601 length = end - start;
603 if (table_en->target->dump == NULL) {
608 table_en->target->dump(table_en,
609 (char *)ap->a_virtual + data_offset,
612 issued_len += end - start;
616 if (issued_len < buf_len)
620 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
627 dmsize(struct dev_psize_args *ap)
629 cdev_t dev = ap->a_head.a_dev;
635 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
638 size = dm_table_size(&dmv->table_head);
641 ap->a_result = (int64_t)size;
648 dmminphys(struct buf *bp)
651 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
656 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
658 struct disk_info info;
661 dmp_size = dm_table_size(head);
663 bzero(&info, sizeof(struct disk_info));
664 info.d_media_blksize = DEV_BSIZE;
665 info.d_media_blocks = dmp_size;
667 /* this is set by disk_setdiskinfo */
668 info.d_media_size = dmp_size * DEV_BSIZE;
670 info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER;
672 info.d_secpertrack = 32;
674 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
675 info.d_ncylinders = dmp_size / info.d_secpercyl;
677 disk_setdiskinfo(disk, &info);
681 dm_builtin_init(void *arg)
683 modeventhand_t evh = (modeventhand_t)arg;
685 KKASSERT(evh != NULL);
686 evh(NULL, MOD_LOAD, NULL);
690 dm_builtin_uninit(void *arg)
692 modeventhand_t evh = (modeventhand_t)arg;
694 KKASSERT(evh != NULL);
695 evh(NULL, MOD_UNLOAD, NULL);
698 TUNABLE_INT("debug.dm_debug", &dm_debug_level);
699 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
700 0, "Eanble device mapper debugging");