1 /* $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */
4 * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5 * Copyright (c) 2010 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * I want to say thank you to all people who helped me with this project.
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/ctype.h>
43 #include <sys/device.h>
45 #include <sys/disklabel.h>
46 #include <sys/dtype.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 /* ***Variable-definitions*** */
76 struct dev_ops dm_ops = {
77 { "dm", 0, D_DISK | D_MPSAFE },
83 .d_strategy = dmstrategy,
89 MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations");
91 int dm_debug_level = 0;
93 extern uint64_t dm_dev_counter;
97 static moduledata_t dm_mod = {
102 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
103 MODULE_VERSION(dm, 1);
106 * This array is used to translate cmd to function pointer.
108 * Interface between libdevmapper and lvm2tools uses different
109 * names for one IOCTL call because libdevmapper do another thing
110 * then. When I run "info" or "mknodes" libdevmapper will send same
111 * ioctl to kernel but will do another things in userspace.
114 static struct cmd_function cmd_fn[] = {
115 { .cmd = "version", .fn = dm_get_version_ioctl},
116 { .cmd = "targets", .fn = dm_list_versions_ioctl},
117 { .cmd = "create", .fn = dm_dev_create_ioctl},
118 { .cmd = "info", .fn = dm_dev_status_ioctl},
119 { .cmd = "mknodes", .fn = dm_dev_status_ioctl},
120 { .cmd = "names", .fn = dm_dev_list_ioctl},
121 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
122 { .cmd = "remove", .fn = dm_dev_remove_ioctl},
123 { .cmd = "remove_all", .fn = dm_dev_remove_all_ioctl},
124 { .cmd = "rename", .fn = dm_dev_rename_ioctl},
125 { .cmd = "resume", .fn = dm_dev_resume_ioctl},
126 { .cmd = "clear", .fn = dm_table_clear_ioctl},
127 { .cmd = "deps", .fn = dm_table_deps_ioctl},
128 { .cmd = "reload", .fn = dm_table_load_ioctl},
129 { .cmd = "status", .fn = dm_table_status_ioctl},
130 { .cmd = "table", .fn = dm_table_status_ioctl},
131 { .cmd = "message", .fn = dm_message_ioctl},
135 /* New module handle routine */
137 dm_modcmd(module_t mod, int cmd, void *unused)
146 kprintf("Device Mapper version %d.%d.%d loaded\n",
147 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL);
152 * Disable unloading of dm module if there are any devices
153 * defined in driver. This is probably too strong we need
154 * to disable auto-unload only if there is mounted dm device
157 if (dm_dev_counter > 0)
163 kprintf("Device Mapper unloaded\n");
179 dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control");
182 /* Destroy routine */
196 dmopen(struct dev_open_args *ap)
198 cdev_t dev = ap->a_head.a_dev;
201 /* Shortcut for the control device */
205 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
211 aprint_debug("dm open routine called %" PRIu32 "\n",
212 minor(ap->a_head.a_dev));
217 dmclose(struct dev_close_args *ap)
219 cdev_t dev = ap->a_head.a_dev;
222 /* Shortcut for the control device */
226 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
232 aprint_debug("dm close routine called %" PRIu32 "\n",
233 minor(ap->a_head.a_dev));
239 dmioctl(struct dev_ioctl_args *ap)
241 cdev_t dev = ap->a_head.a_dev;
242 u_long cmd = ap->a_cmd;
243 void *data = ap->a_data;
246 prop_dictionary_t dm_dict_in;
250 aprint_debug("dmioctl called\n");
252 KKASSERT(data != NULL);
254 if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
255 struct plistref *pref = (struct plistref *) data;
257 /* Check if we were called with NETBSD_DM_IOCTL ioctl
259 if ((r = dm_ioctl_switch(cmd)) != 0)
262 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
265 if ((r = dm_check_version(dm_dict_in)) != 0)
268 /* run ioctl routine */
269 if ((err = dm_cmd_to_fun(dm_dict_in)) != 0)
273 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
274 prop_object_release(dm_dict_in);
278 * Return the error of the actual command if one one has
279 * happened. Otherwise return 'r' which indicates errors
280 * that occurred during helper operations.
282 return (err != 0)?err:r;
286 * Translate command sent from libdevmapper to func.
289 dm_cmd_to_fun(prop_dictionary_t dm_dict){
291 prop_string_t command;
295 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
298 for(i = 0; cmd_fn[i].cmd != NULL; i++)
299 if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
302 if (cmd_fn[i].cmd == NULL)
305 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
306 r = cmd_fn[i].fn(dm_dict);
311 /* Call apropriate ioctl handler function. */
313 dm_ioctl_switch(u_long cmd)
318 case NETBSD_DM_IOCTL:
319 aprint_debug("dm NetBSD_DM_IOCTL called\n");
322 aprint_debug("dm unknown ioctl called\n");
324 break; /* NOT REACHED */
331 * Check for disk specific ioctls.
335 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data)
339 /* disk ioctls make sense only on block devices */
346 struct partinfo *dpart;
349 bzero(dpart, sizeof(*dpart));
351 if ((dmv = dev->si_drv1) == NULL)
353 if (dmv->diskp->d_info.d_media_blksize == 0) {
356 size = dm_table_size(&dmv->table_head);
357 dpart->media_offset = 0;
358 dpart->media_size = size * DEV_BSIZE;
359 dpart->media_blocks = size;
360 dpart->media_blksize = DEV_BSIZE;
361 dpart->fstype = FS_BSDFFS;
367 aprint_debug("unknown disk_ioctl called\n");
369 break; /* NOT REACHED */
376 * Do all IO operations on dm logical devices.
379 dmstrategy(struct dev_strategy_args *ap)
381 cdev_t dev = ap->a_head.a_dev;
382 struct bio *bio = ap->a_bio;
383 struct buf *bp = bio->bio_buf;
388 dm_table_entry_t *table_en;
391 uint64_t buf_start, buf_len, issued_len;
392 uint64_t table_start, table_end;
395 buf_start = bio->bio_offset;
396 buf_len = bp->b_bcount;
408 case BUF_CMD_FREEBLKS:
413 KKASSERT(buf_len == 0);
417 bp->b_resid = bp->b_bcount;
423 bounds_check_with_mediasize(bio, DEV_BSIZE,
424 dm_table_size(&dmv->table_head)) <= 0) {
425 bp->b_resid = bp->b_bcount;
430 /* Select active table */
431 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
434 devstat_start_transaction(&dmv->stats);
437 * Find out what tables I want to select.
439 SLIST_FOREACH(table_en, tbl, next) {
441 * I need need number of bytes not blocks.
443 table_start = table_en->start * DEV_BSIZE;
444 table_end = table_start + (table_en->length) * DEV_BSIZE;
447 * Calculate the start and end
449 start = MAX(table_start, buf_start);
450 end = MIN(table_end, buf_start + buf_len);
452 aprint_debug("----------------------------------------\n");
453 aprint_debug("table_start %010" PRIu64", table_end %010"
454 PRIu64 "\n", table_start, table_end);
455 aprint_debug("buf_start %010" PRIu64", buf_len %010"
456 PRIu64"\n", buf_start, buf_len);
457 aprint_debug("start-buf_start %010"PRIu64", end %010"
458 PRIu64"\n", start - buf_start, end);
459 aprint_debug("start %010" PRIu64" , end %010"
460 PRIu64"\n", start, end);
461 aprint_debug("\n----------------------------------------\n");
464 nestbuf = getpbuf(NULL);
465 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
467 nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats);
468 nestbuf->b_bio1.bio_offset = 0;
469 table_en->target->strategy(table_en, nestbuf);
470 } else if (start < end) {
471 nestbuf = getpbuf(NULL);
472 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS;
474 nestiobuf_add(bio, nestbuf,
475 start - buf_start, (end - start),
477 issued_len += end - start;
479 nestbuf->b_bio1.bio_offset = (start - table_start);
480 table_en->target->strategy(table_en, nestbuf);
484 if (issued_len < buf_len)
485 nestiobuf_error(bio, EINVAL);
486 nestiobuf_start(bio);
487 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
493 dmdump(struct dev_dump_args *ap)
495 cdev_t dev = ap->a_head.a_dev;
498 dm_table_entry_t *table_en;
499 uint64_t buf_start, buf_len, issued_len;
500 uint64_t table_start, table_end;
501 uint64_t start, end, data_offset;
506 buf_start = ap->a_offset;
507 buf_len = ap->a_length;
516 /* Select active table */
517 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
521 * Find out what tables I want to select.
523 SLIST_FOREACH(table_en, tbl, next) {
525 * I need need number of bytes not blocks.
527 table_start = table_en->start * DEV_BSIZE;
528 table_end = table_start + (table_en->length) * DEV_BSIZE;
531 * Calculate the start and end
533 start = MAX(table_start, buf_start);
534 end = MIN(table_end, buf_start + buf_len);
536 if (ap->a_length == 0) {
537 if (table_en->target->dump == NULL) {
542 table_en->target->dump(table_en, NULL, 0, 0);
543 } else if (start < end) {
544 data_offset = start - buf_start;
545 offset = start - table_start;
546 length = end - start;
548 if (table_en->target->dump == NULL) {
553 table_en->target->dump(table_en,
554 (char *)ap->a_virtual + data_offset,
557 issued_len += end - start;
561 if (issued_len < buf_len)
565 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
571 dmsize(struct dev_psize_args *ap)
573 cdev_t dev = ap->a_head.a_dev;
579 if ((dmv = dev->si_drv1) == NULL)
582 size = dm_table_size(&dmv->table_head);
583 ap->a_result = (int64_t)size;
590 dmminphys(struct buf *bp)
593 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
598 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
600 struct disk_info info;
603 dmp_size = dm_table_size(head);
605 bzero(&info, sizeof(struct disk_info));
606 info.d_media_blksize = DEV_BSIZE;
607 info.d_media_blocks = dmp_size;
609 /* this is set by disk_setdiskinfo */
610 info.d_media_size = dmp_size * DEV_BSIZE;
612 info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER | DSO_RAWPSIZE;
614 info.d_secpertrack = 32;
616 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
617 info.d_ncylinders = dmp_size / info.d_secpercyl;
619 disk_setdiskinfo(disk, &info);
623 * Transform char s to uint64_t offset number.
626 atoi64(const char *s)
635 n = (10 * n) + (*s - '0');
643 dm_builtin_init(void *arg)
645 modeventhand_t evh = (modeventhand_t)arg;
647 KKASSERT(evh != NULL);
648 evh(NULL, MOD_LOAD, NULL);
652 dm_builtin_uninit(void *arg)
654 modeventhand_t evh = (modeventhand_t)arg;
656 KKASSERT(evh != NULL);
657 evh(NULL, MOD_UNLOAD, NULL);
660 TUNABLE_INT("debug.dm_debug", &dm_debug_level);
661 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
662 0, "Eanble device mapper debugging");