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/ctype.h>
39 #include <sys/device.h>
41 #include <sys/disklabel.h>
42 #include <sys/dtype.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <dev/disk/dm/dm.h>
47 #include <dev/disk/dm/netbsd-dm.h>
49 static d_ioctl_t dmioctl
;
50 static d_open_t dmopen
;
51 static d_close_t dmclose
;
52 static d_psize_t dmsize
;
53 static d_strategy_t dmstrategy
;
54 static d_dump_t dmdump
;
56 /* New module handle and destroy routines */
57 static int dm_modcmd(module_t mod
, int cmd
, void *unused
);
58 static int dmdestroy(void);
60 static void dm_doinit(void);
62 static int dm_cmd_to_fun(prop_dictionary_t
);
63 static int disk_ioctl_switch(cdev_t
, u_long
, void *);
64 static int dm_ioctl_switch(u_long
);
66 static struct dev_ops dmctl_ops
= {
67 { "dm", 0, D_MPSAFE
},
73 struct dev_ops dm_ops
= {
74 { "dm", 0, D_DISK
| D_MPSAFE
},
80 .d_strategy
= dmstrategy
,
85 MALLOC_DEFINE(M_DM
, "dm", "Device Mapper allocations");
87 int dm_debug_level
= 0;
89 extern uint64_t dm_dev_counter
;
93 static moduledata_t dm_mod
= {
98 DECLARE_MODULE(dm
, dm_mod
, SI_SUB_RAID
, SI_ORDER_ANY
);
99 MODULE_VERSION(dm
, 1);
102 * This structure is used to translate command sent to kernel driver in
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.
115 static struct cmd_function
{
117 int (*fn
)(prop_dictionary_t
);
119 {.cmd
= "version", .fn
= NULL
},
120 {.cmd
= "targets", .fn
= dm_list_versions_ioctl
},
121 {.cmd
= "create", .fn
= dm_dev_create_ioctl
},
122 {.cmd
= "info", .fn
= dm_dev_status_ioctl
},
123 {.cmd
= "mknodes", .fn
= dm_dev_status_ioctl
},
124 {.cmd
= "names", .fn
= dm_dev_list_ioctl
},
125 {.cmd
= "suspend", .fn
= dm_dev_suspend_ioctl
},
126 {.cmd
= "remove", .fn
= dm_dev_remove_ioctl
},
127 {.cmd
= "remove_all", .fn
= dm_dev_remove_all_ioctl
},
128 {.cmd
= "rename", .fn
= dm_dev_rename_ioctl
},
129 {.cmd
= "resume", .fn
= dm_dev_resume_ioctl
},
130 {.cmd
= "clear", .fn
= dm_table_clear_ioctl
},
131 {.cmd
= "deps", .fn
= dm_table_deps_ioctl
},
132 {.cmd
= "reload", .fn
= dm_table_load_ioctl
},
133 {.cmd
= "status", .fn
= dm_table_status_ioctl
},
134 {.cmd
= "table", .fn
= dm_table_status_ioctl
},
135 {.cmd
= "message", .fn
= dm_message_ioctl
},
139 * New module handle routine
142 dm_modcmd(module_t mod
, int cmd
, void *unused
)
151 kprintf("Device Mapper version %d.%d.%d loaded\n",
152 DM_VERSION_MAJOR
, DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
);
157 * Disable unloading of dm module if there are any devices
158 * defined in driver. This is probably too strong we need
159 * to disable auto-unload only if there is mounted dm device
162 if (dm_dev_counter
> 0)
164 /* race window here */
169 kprintf("Device Mapper unloaded\n");
182 dmcdev
= make_dev(&dmctl_ops
, 0, UID_ROOT
, GID_OPERATOR
, 0640, "mapper/control");
201 dmopen(struct dev_open_args
*ap
)
203 cdev_t dev
= ap
->a_head
.a_dev
;
206 /* Shortcut for the control device */
210 if ((dmv
= dm_dev_lookup(NULL
, NULL
, minor(dev
))) == NULL
)
216 dmdebug("minor=%" PRIu32
"\n", minor(ap
->a_head
.a_dev
));
221 dmclose(struct dev_close_args
*ap
)
223 cdev_t dev
= ap
->a_head
.a_dev
;
226 /* Shortcut for the control device */
230 if ((dmv
= dm_dev_lookup(NULL
, NULL
, minor(dev
))) == NULL
)
236 dmdebug("minor=%" PRIu32
"\n", minor(ap
->a_head
.a_dev
));
242 dmioctl(struct dev_ioctl_args
*ap
)
244 cdev_t dev
= ap
->a_head
.a_dev
;
245 u_long cmd
= ap
->a_cmd
;
246 void *data
= ap
->a_data
;
247 struct plistref
*pref
;
250 prop_dictionary_t dm_dict_in
;
253 KKASSERT(data
!= NULL
);
255 if ((r
= disk_ioctl_switch(dev
, cmd
, data
)) != ENOTTY
)
256 return r
; /* Handled disk ioctl */
258 if ((r
= dm_ioctl_switch(cmd
)) != 0)
259 return r
; /* Not NETBSD_DM_IOCTL */
261 pref
= (struct plistref
*)data
; /* data is for libprop */
262 if ((r
= prop_dictionary_copyin_ioctl(pref
, cmd
, &dm_dict_in
)) != 0)
265 if ((r
= dm_check_version(dm_dict_in
)) == 0)
266 err
= dm_cmd_to_fun(dm_dict_in
);
268 r
= prop_dictionary_copyout_ioctl(pref
, cmd
, dm_dict_in
);
269 prop_object_release(dm_dict_in
);
271 /* Return the dm ioctl error if any. */
278 * Translate command sent from libdevmapper to func.
281 dm_cmd_to_fun(prop_dictionary_t dm_dict
)
284 prop_string_t command
;
285 struct cmd_function
*p
= NULL
;
287 size
= sizeof(cmd_fn
) / sizeof(cmd_fn
[0]);
289 if ((command
= prop_dictionary_get(dm_dict
, DM_IOCTL_COMMAND
)) == NULL
)
292 for (i
= 0; i
< size
; i
++) {
294 if (prop_string_equals_cstring(command
, p
->cmd
))
300 dmdebug("Unknown ioctl\n");
304 dmdebug("ioctl %s called %p\n", p
->cmd
, p
->fn
);
306 return 0; /* No handler required */
308 return p
->fn(dm_dict
);
312 * Call apropriate ioctl handler function.
315 dm_ioctl_switch(u_long cmd
)
319 case NETBSD_DM_IOCTL
:
320 dmdebug("NETBSD_DM_IOCTL called\n");
323 dmdebug("Unknown ioctl %lu called\n", cmd
);
325 break; /* NOT REACHED */
332 * 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 */
345 if ((dmv
= dev
->si_drv1
) == NULL
)
347 if (dmv
->diskp
->d_info
.d_media_blksize
== 0) {
351 struct partinfo
*dpart
= data
;
352 bzero(dpart
, sizeof(*dpart
));
354 size
= dm_table_size(&dmv
->table_head
);
355 dpart
->media_offset
= 0;
356 dpart
->media_size
= size
* DEV_BSIZE
;
357 dpart
->media_blocks
= size
;
358 dpart
->media_blksize
= DEV_BSIZE
;
359 dpart
->fstype
= FS_BSDFFS
;
361 dmdebug("DIOCGPART called\n");
365 dmdebug("Unknown disk ioctl %lu called\n", cmd
);
367 break; /* NOT REACHED */
374 * Do all IO operations on dm logical devices.
377 dmstrategy(struct dev_strategy_args
*ap
)
379 cdev_t dev
= ap
->a_head
.a_dev
;
380 dm_dev_t
*dmv
= dev
->si_drv1
;
382 dm_table_entry_t
*table_en
;
384 struct bio
*bio
= ap
->a_bio
;
385 struct buf
*bp
= bio
->bio_buf
;
388 uint64_t buf_start
, buf_len
, issued_len
;
389 uint64_t table_start
, table_end
;
393 buf_start
= bio
->bio_offset
;
394 buf_len
= bp
->b_bcount
;
400 case BUF_CMD_FREEBLKS
:
405 KKASSERT(buf_len
== 0);
409 bp
->b_resid
= bp
->b_bcount
;
415 bounds_check_with_mediasize(bio
, DEV_BSIZE
,
416 dm_table_size(&dmv
->table_head
)) <= 0) {
417 bp
->b_resid
= bp
->b_bcount
;
422 /* Select active table */
423 tbl
= dm_table_get_entry(&dmv
->table_head
, DM_TABLE_ACTIVE
);
426 devstat_start_transaction(&dmv
->stats
);
429 * Find out what tables I want to select.
431 TAILQ_FOREACH(table_en
, tbl
, next
) {
433 * I need need number of bytes not blocks.
435 table_start
= table_en
->start
* DEV_BSIZE
;
436 table_end
= table_start
+ table_en
->length
* DEV_BSIZE
;
439 * Calculate the start and end
441 start
= MAX(table_start
, buf_start
);
442 end
= MIN(table_end
, buf_start
+ buf_len
);
444 if (dm_debug_level
) {
445 kprintf("----------------------------------------\n");
446 kprintf("table_start %010" PRIu64
", table_end %010"
447 PRIu64
"\n", table_start
, table_end
);
448 kprintf("buf_start %010" PRIu64
", buf_len %010"
449 PRIu64
"\n", buf_start
, buf_len
);
450 kprintf("start-buf_start %010"PRIu64
", end %010"
451 PRIu64
"\n", start
- buf_start
, end
);
452 kprintf("start %010" PRIu64
", end %010"
453 PRIu64
"\n", start
, end
);
457 nestbuf
= getpbuf(NULL
);
458 nestbuf
->b_flags
|= bio
->bio_buf
->b_flags
& B_HASBOGUS
;
460 nestiobuf_add(bio
, nestbuf
, 0, 0, &dmv
->stats
);
461 nestbuf
->b_bio1
.bio_offset
= 0;
462 table_en
->target
->strategy(table_en
, nestbuf
);
463 } else if (start
< end
) {
464 nestbuf
= getpbuf(NULL
);
465 nestbuf
->b_flags
|= bio
->bio_buf
->b_flags
& B_HASBOGUS
;
467 nestiobuf_add(bio
, nestbuf
,
468 start
- buf_start
, end
- start
,
470 issued_len
+= end
- start
;
472 nestbuf
->b_bio1
.bio_offset
= start
- table_start
;
473 table_en
->target
->strategy(table_en
, nestbuf
);
477 if (issued_len
< buf_len
)
478 nestiobuf_error(bio
, EINVAL
);
479 nestiobuf_start(bio
);
480 dm_table_release(&dmv
->table_head
, DM_TABLE_ACTIVE
);
486 dmdump(struct dev_dump_args
*ap
)
488 cdev_t dev
= ap
->a_head
.a_dev
;
489 dm_dev_t
*dmv
= dev
->si_drv1
;
491 dm_table_entry_t
*table_en
;
493 uint64_t buf_start
, buf_len
, issued_len
;
494 uint64_t table_start
, table_end
;
498 buf_start
= ap
->a_offset
;
499 buf_len
= ap
->a_length
;
502 /* Select active table */
503 tbl
= dm_table_get_entry(&dmv
->table_head
, DM_TABLE_ACTIVE
);
506 * Find out what tables I want to select.
508 TAILQ_FOREACH(table_en
, tbl
, next
) {
510 * I need need number of bytes not blocks.
512 table_start
= table_en
->start
* DEV_BSIZE
;
513 table_end
= table_start
+ table_en
->length
* DEV_BSIZE
;
516 * Calculate the start and end
518 start
= MAX(table_start
, buf_start
);
519 end
= MIN(table_end
, buf_start
+ buf_len
);
521 if (ap
->a_length
== 0) {
522 if (table_en
->target
->dump
== NULL
) {
527 table_en
->target
->dump(table_en
, NULL
, 0, 0);
528 } else if (start
< end
) {
529 if (table_en
->target
->dump
== NULL
) {
534 table_en
->target
->dump(table_en
,
535 (char *)ap
->a_virtual
+ start
- buf_start
,
536 end
- start
, start
- table_start
);
538 issued_len
+= end
- start
;
542 if (issued_len
< buf_len
)
546 dm_table_release(&dmv
->table_head
, DM_TABLE_ACTIVE
);
552 dmsize(struct dev_psize_args
*ap
)
554 cdev_t dev
= ap
->a_head
.a_dev
;
557 if ((dmv
= dev
->si_drv1
) == NULL
)
560 ap
->a_result
= (int64_t)dm_table_size(&dmv
->table_head
);
566 dmsetdiskinfo(struct disk
*disk
, dm_table_head_t
*head
)
568 struct disk_info info
;
571 dmp_size
= dm_table_size(head
);
573 bzero(&info
, sizeof(struct disk_info
));
574 info
.d_media_blksize
= DEV_BSIZE
;
575 info
.d_media_blocks
= dmp_size
;
577 /* this is set by disk_setdiskinfo */
578 info
.d_media_size
= dmp_size
* DEV_BSIZE
;
580 info
.d_dsflags
= DSO_MBRQUIET
| DSO_DEVICEMAPPER
| DSO_RAWPSIZE
;
582 info
.d_secpertrack
= 32;
584 info
.d_secpercyl
= info
.d_secpertrack
* info
.d_nheads
;
585 info
.d_ncylinders
= dmp_size
/ info
.d_secpercyl
;
587 disk_setdiskinfo(disk
, &info
);
591 * Transform char s to uint64_t offset number.
594 atoi64(const char *s
)
603 n
= (10 * n
) + (*s
- '0');
611 dm_alloc_string(int len
)
614 len
= DM_MAX_PARAMS_SIZE
;
615 return kmalloc(len
, M_DM
, M_WAITOK
| M_ZERO
);
619 dm_builtin_init(void *arg
)
621 modeventhand_t evh
= (modeventhand_t
)arg
;
623 KKASSERT(evh
!= NULL
);
624 evh(NULL
, MOD_LOAD
, NULL
);
628 dm_builtin_uninit(void *arg
)
630 modeventhand_t evh
= (modeventhand_t
)arg
;
632 KKASSERT(evh
!= NULL
);
633 evh(NULL
, MOD_UNLOAD
, NULL
);
636 TUNABLE_INT("debug.dm_debug", &dm_debug_level
);
637 SYSCTL_INT(_debug
, OID_AUTO
, dm_debug
, CTLFLAG_RW
, &dm_debug_level
,
638 0, "Enable device mapper debugging");