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>
43 #include <sys/disklabel.h>
44 #include <sys/dtype.h>
45 #include <sys/ioccom.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/sysctl.h>
50 #include "netbsd-dm.h"
53 static d_ioctl_t dmioctl
;
54 static d_open_t dmopen
;
55 static d_close_t dmclose
;
56 static d_psize_t dmsize
;
57 static d_strategy_t dmstrategy
;
59 /* attach and detach routines */
61 static int dm_modcmd(module_t mod
, int cmd
, void *unused
);
62 static int dmdestroy(void);
64 static void dm_doinit(void);
66 static int dm_cmd_to_fun(prop_dictionary_t
);
67 static int disk_ioctl_switch(cdev_t
, u_long
, void *);
68 static int dm_ioctl_switch(u_long
);
70 static void dmminphys(struct buf
*);
73 /* ***Variable-definitions*** */
74 struct dev_ops dm_ops
= {
76 D_MPSAFE_READ
| D_MPSAFE_WRITE
| D_MPSAFE_IOCTL
},
82 .d_strategy
= dmstrategy
,
87 MALLOC_DEFINE(M_DM
, "dm", "Device Mapper allocations");
89 int dm_debug_level
= 0;
91 extern uint64_t dm_dev_counter
;
95 static moduledata_t dm_mod
= {
100 DECLARE_MODULE(dm
, dm_mod
, SI_SUB_RAID
, SI_ORDER_ANY
);
103 * This array is used to translate cmd to function pointer.
105 * Interface between libdevmapper and lvm2tools uses different
106 * names for one IOCTL call because libdevmapper do another thing
107 * then. When I run "info" or "mknodes" libdevmapper will send same
108 * ioctl to kernel but will do another things in userspace.
111 static struct cmd_function cmd_fn
[] = {
112 { .cmd
= "version", .fn
= dm_get_version_ioctl
},
113 { .cmd
= "targets", .fn
= dm_list_versions_ioctl
},
114 { .cmd
= "create", .fn
= dm_dev_create_ioctl
},
115 { .cmd
= "info", .fn
= dm_dev_status_ioctl
},
116 { .cmd
= "mknodes", .fn
= dm_dev_status_ioctl
},
117 { .cmd
= "names", .fn
= dm_dev_list_ioctl
},
118 { .cmd
= "suspend", .fn
= dm_dev_suspend_ioctl
},
119 { .cmd
= "remove", .fn
= dm_dev_remove_ioctl
},
120 { .cmd
= "rename", .fn
= dm_dev_rename_ioctl
},
121 { .cmd
= "resume", .fn
= dm_dev_resume_ioctl
},
122 { .cmd
= "clear", .fn
= dm_table_clear_ioctl
},
123 { .cmd
= "deps", .fn
= dm_table_deps_ioctl
},
124 { .cmd
= "reload", .fn
= dm_table_load_ioctl
},
125 { .cmd
= "status", .fn
= dm_table_status_ioctl
},
126 { .cmd
= "table", .fn
= dm_table_status_ioctl
},
130 /* New module handle routine */
132 dm_modcmd(module_t mod
, int cmd
, void *unused
)
134 int error
, bmajor
, cmajor
;
143 kprintf("Device Mapper version %d.%d.%d loaded\n",
144 DM_VERSION_MAJOR
, DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
);
149 * Disable unloading of dm module if there are any devices
150 * defined in driver. This is probably too strong we need
151 * to disable auto-unload only if there is mounted dm device
154 if (dm_dev_counter
> 0)
160 kprintf("Device Mapper unloaded\n");
173 * Autoconfiguration detach function for pseudo-device glue.
174 * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
175 * remove devices created in device-mapper.
178 dm_detach(dm_dev_t
*dmv
)
182 /* Destroy active table first. */
183 dm_table_destroy(&dmv
->table_head
, DM_TABLE_ACTIVE
);
185 /* Destroy inactive table if exits, too. */
186 dm_table_destroy(&dmv
->table_head
, DM_TABLE_INACTIVE
);
188 dm_table_head_destroy(&dmv
->table_head
);
190 destroy_dev(dmv
->devt
);
193 (void)dm_dev_free(dmv
);
195 /* Decrement device counter After removing device */
196 --dm_dev_counter
; /* XXX: was atomic 64 */
207 dmcdev
= make_dev(&dm_ops
, 0, UID_ROOT
, GID_OPERATOR
, 0640, "mapper/control");
210 /* Destroy routine */
224 dmopen(struct dev_open_args
*ap
)
227 aprint_debug("dm open routine called %" PRIu32
"\n",
228 minor(ap
->a_head
.a_dev
));
233 dmclose(struct dev_close_args
*ap
)
236 aprint_debug("dm close routine called %" PRIu32
"\n",
237 minor(ap
->a_head
.a_dev
));
243 dmioctl(struct dev_ioctl_args
*ap
)
245 cdev_t dev
= ap
->a_head
.a_dev
;
246 u_long cmd
= ap
->a_cmd
;
247 void *data
= ap
->a_data
;
250 prop_dictionary_t dm_dict_in
;
254 aprint_debug("dmioctl called\n");
256 KKASSERT(data
!= NULL
);
258 if (( r
= disk_ioctl_switch(dev
, cmd
, data
)) == ENOTTY
) {
259 struct plistref
*pref
= (struct plistref
*) data
;
261 /* Check if we were called with NETBSD_DM_IOCTL ioctl
263 if ((r
= dm_ioctl_switch(cmd
)) != 0)
266 if((r
= prop_dictionary_copyin_ioctl(pref
, cmd
, &dm_dict_in
)) != 0)
269 if ((r
= dm_check_version(dm_dict_in
)) != 0)
272 /* run ioctl routine */
273 if ((r
= dm_cmd_to_fun(dm_dict_in
)) != 0)
277 r
= prop_dictionary_copyout_ioctl(pref
, cmd
, dm_dict_in
);
278 prop_object_release(dm_dict_in
);
285 * Translate command sent from libdevmapper to func.
288 dm_cmd_to_fun(prop_dictionary_t dm_dict
){
290 prop_string_t command
;
294 if ((command
= prop_dictionary_get(dm_dict
, DM_IOCTL_COMMAND
)) == NULL
)
297 for(i
= 0; cmd_fn
[i
].cmd
!= NULL
; i
++)
298 if (prop_string_equals_cstring(command
, cmd_fn
[i
].cmd
))
301 if (cmd_fn
[i
].cmd
== NULL
)
304 aprint_debug("ioctl %s called\n", cmd_fn
[i
].cmd
);
305 r
= cmd_fn
[i
].fn(dm_dict
);
310 /* Call apropriate ioctl handler function. */
312 dm_ioctl_switch(u_long cmd
)
317 case NETBSD_DM_IOCTL
:
318 aprint_debug("dm NetBSD_DM_IOCTL called\n");
321 aprint_debug("dm unknown ioctl called\n");
323 break; /* NOT REACHED */
330 * Check for disk specific ioctls.
334 disk_ioctl_switch(cdev_t dev
, u_long cmd
, void *data
)
338 /* disk ioctls make sense only on block devices */
345 struct partinfo
*dpart
;
347 dpart
= (void *)data
;
348 bzero(dpart
, sizeof(*dpart
));
350 if ((dmv
= dm_dev_lookup(NULL
, NULL
, minor(dev
))) == NULL
)
352 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
;
368 aprint_debug("unknown disk_ioctl called\n");
370 break; /* NOT REACHED */
377 * Do all IO operations on dm logical devices.
380 dmstrategy(struct dev_strategy_args
*ap
)
382 cdev_t dev
= ap
->a_head
.a_dev
;
383 struct bio
*bio
= ap
->a_bio
;
384 struct buf
*bp
= bio
->bio_buf
;
388 dm_table_entry_t
*table_en
;
393 uint64_t buf_start
, buf_len
, issued_len
;
394 uint64_t table_start
, table_end
;
397 buf_start
= bio
->bio_offset
;
398 buf_len
= bp
->b_bcount
;
406 if ((dmv
= dm_dev_lookup(NULL
, NULL
, minor(dev
))) == NULL
) {
408 bp
->b_resid
= bp
->b_bcount
;
413 if (bounds_check_with_mediasize(bio
, DEV_BSIZE
,
414 dm_table_size(&dmv
->table_head
)) <= 0) {
416 bp
->b_resid
= bp
->b_bcount
;
421 /* Select active table */
422 tbl
= dm_table_get_entry(&dmv
->table_head
, DM_TABLE_ACTIVE
);
424 /* Nested buffers count down to zero therefore I have
425 to set bp->b_resid to maximal value. */
426 bp
->b_resid
= bp
->b_bcount
;
429 * Find out what tables I want to select.
431 SLIST_FOREACH(table_en
, tbl
, next
)
433 /* I need need number of bytes not blocks. */
434 table_start
= table_en
->start
* DEV_BSIZE
;
436 * I have to sub 1 from table_en->length to prevent
439 table_end
= table_start
+ (table_en
->length
)* DEV_BSIZE
;
441 start
= MAX(table_start
, buf_start
);
443 end
= MIN(table_end
, buf_start
+ buf_len
);
445 aprint_debug("----------------------------------------\n");
446 aprint_debug("table_start %010" PRIu64
", table_end %010"
447 PRIu64
"\n", table_start
, table_end
);
448 aprint_debug("buf_start %010" PRIu64
", buf_len %010"
449 PRIu64
"\n", buf_start
, buf_len
);
450 aprint_debug("start-buf_start %010"PRIu64
", end %010"
451 PRIu64
"\n", start
- buf_start
, end
);
452 aprint_debug("start %010" PRIu64
" , end %010"
453 PRIu64
"\n", start
, end
);
454 aprint_debug("\n----------------------------------------\n");
457 /* create nested buffer */
458 nestbuf
= getpbuf(NULL
);
460 nestiobuf_setup(bio
, nestbuf
, start
- buf_start
,
463 issued_len
+= end
- start
;
465 /* I need number of bytes. */
466 nestbuf
->b_bio1
.bio_offset
= (start
- table_start
);
468 table_en
->target
->strategy(table_en
, nestbuf
);
472 if (issued_len
< buf_len
)
473 nestiobuf_done(bio
, buf_len
- issued_len
, EINVAL
);
475 dm_table_release(&dmv
->table_head
, DM_TABLE_ACTIVE
);
482 dmsize(struct dev_psize_args
*ap
)
484 cdev_t dev
= ap
->a_head
.a_dev
;
490 if ((dmv
= dm_dev_lookup(NULL
, NULL
, minor(dev
))) == NULL
)
493 size
= dm_table_size(&dmv
->table_head
);
501 dmminphys(struct buf
*bp
)
504 bp
->b_bcount
= MIN(bp
->b_bcount
, MAXPHYS
);
509 dmsetdiskinfo(struct disk
*disk
, dm_table_head_t
*head
)
511 struct disk_info info
;
514 dmp_size
= dm_table_size(head
);
516 bzero(&info
, sizeof(struct disk_info
));
517 info
.d_media_blksize
= DEV_BSIZE
;
518 info
.d_media_blocks
= dmp_size
;
519 info
.d_media_size
= dmp_size
* DEV_BSIZE
;
520 info
.d_dsflags
= DSO_MBRQUIET
; /* XXX */
521 info
.d_secpertrack
= 32;
523 info
.d_secpercyl
= info
.d_secpertrack
* info
.d_nheads
;
524 info
.d_ncylinders
= dmp_size
/ 2048;
525 bcopy(&info
, &disk
->d_info
, sizeof(disk
->d_info
));
529 dmgetdiskinfo(struct disk
*disk
)
531 prop_dictionary_t disk_info
, geom
;
532 struct disk_info
*pinfo
;
534 pinfo
= &disk
->d_info
;
536 disk_info
= prop_dictionary_create();
537 geom
= prop_dictionary_create();
539 prop_dictionary_set_cstring_nocopy(disk_info
, "type", "ESDI");
540 prop_dictionary_set_uint64(geom
, "sectors-per-unit", pinfo
->d_media_blocks
);
541 prop_dictionary_set_uint32(geom
, "sector-size",
542 DEV_BSIZE
/* XXX 512? */);
543 prop_dictionary_set_uint32(geom
, "sectors-per-track", 32);
544 prop_dictionary_set_uint32(geom
, "tracks-per-cylinder", 64);
545 prop_dictionary_set_uint32(geom
, "cylinders-per-unit",
546 pinfo
->d_media_blocks
/ 2048);
547 prop_dictionary_set(disk_info
, "geometry", geom
);
548 prop_object_release(geom
);
554 dmgetproperties(struct disk
*disk
, dm_table_head_t
*head
)
557 prop_dictionary_t disk_info
, odisk_info
, geom
;
560 dmp_size
= dm_table_size(head
);
561 disk_info
= prop_dictionary_create();
562 geom
= prop_dictionary_create();
564 prop_dictionary_set_cstring_nocopy(disk_info
, "type", "ESDI");
565 prop_dictionary_set_uint64(geom
, "sectors-per-unit", dmp_size
);
566 prop_dictionary_set_uint32(geom
, "sector-size",
567 DEV_BSIZE
/* XXX 512? */);
568 prop_dictionary_set_uint32(geom
, "sectors-per-track", 32);
569 prop_dictionary_set_uint32(geom
, "tracks-per-cylinder", 64);
570 prop_dictionary_set_uint32(geom
, "cylinders-per-unit", dmp_size
/ 2048);
571 prop_dictionary_set(disk_info
, "geometry", geom
);
572 prop_object_release(geom
);
574 odisk_info
= disk
->dk_info
;
575 disk
->dk_info
= disk_info
;
577 if (odisk_info
!= NULL
)
578 prop_object_release(odisk_info
);
582 TUNABLE_INT("debug.dm_debug", &dm_debug_level
);
583 SYSCTL_INT(_debug
, OID_AUTO
, dm_debug
, CTLFLAG_RW
, &dm_debug_level
,
584 0, "Eanble device mapper debugging");