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>
44 #include <sys/disklabel.h>
45 #include <sys/ioctl.h>
46 #include <sys/ioccom.h>
49 #include "netbsd-dm.h"
52 static dev_type_open(dmopen);
53 static dev_type_close(dmclose);
54 static dev_type_read(dmread);
55 static dev_type_write(dmwrite);
56 static dev_type_ioctl(dmioctl);
57 static dev_type_strategy(dmstrategy);
58 static dev_type_size(dmsize);
60 /* attach and detach routines */
63 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(dev_t, u_long, void *);
70 static int dm_ioctl_switch(u_long);
71 static void dmminphys(struct buf *);
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 *);
78 /* ***Variable-definitions*** */
79 const struct bdevsw dm_bdevsw = {
82 .d_strategy = dmstrategy,
86 .d_flag = D_DISK | D_MPSAFE
89 const struct cdevsw dm_cdevsw = {
99 .d_kqfilter = nokqfilter,
100 .d_flag = D_DISK | D_MPSAFE
103 const struct dkdriver dmdkdriver = {
104 .d_strategy = dmstrategy
107 CFATTACH_DECL3_NEW(dm, 0,
108 dm_match, dm_attach, dm_detach, NULL, NULL, NULL,
109 DVF_DETACH_SHUTDOWN);
111 extern struct cfdriver dm_cd;
113 extern uint64_t dm_dev_counter;
116 * This array is used to translate cmd to function pointer.
118 * Interface between libdevmapper and lvm2tools uses different
119 * names for one IOCTL call because libdevmapper do another thing
120 * then. When I run "info" or "mknodes" libdevmapper will send same
121 * ioctl to kernel but will do another things in userspace.
124 struct cmd_function cmd_fn[] = {
125 { .cmd = "version", .fn = dm_get_version_ioctl},
126 { .cmd = "targets", .fn = dm_list_versions_ioctl},
127 { .cmd = "create", .fn = dm_dev_create_ioctl},
128 { .cmd = "info", .fn = dm_dev_status_ioctl},
129 { .cmd = "mknodes", .fn = dm_dev_status_ioctl},
130 { .cmd = "names", .fn = dm_dev_list_ioctl},
131 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
132 { .cmd = "remove", .fn = dm_dev_remove_ioctl},
133 { .cmd = "rename", .fn = dm_dev_rename_ioctl},
134 { .cmd = "resume", .fn = dm_dev_resume_ioctl},
135 { .cmd = "clear", .fn = dm_table_clear_ioctl},
136 { .cmd = "deps", .fn = dm_table_deps_ioctl},
137 { .cmd = "reload", .fn = dm_table_load_ioctl},
138 { .cmd = "status", .fn = dm_table_status_ioctl},
139 { .cmd = "table", .fn = dm_table_status_ioctl},
144 #include <sys/module.h>
146 /* Autoconf defines */
147 CFDRIVER_DECL(dm, DV_DISK, NULL);
149 MODULE(MODULE_CLASS_DRIVER, dm, NULL);
151 /* New module handle routine */
153 dm_modcmd(modcmd_t cmd, void *arg)
155 int error, bmajor, cmajor;
162 case MODULE_CMD_INIT:
163 error = config_cfdriver_attach(&dm_cd);
167 error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
169 aprint_error("%s: unable to register cfattach\n",
174 error = devsw_attach(dm_cd.cd_name, &dm_bdevsw, &bmajor,
175 &dm_cdevsw, &cmajor);
177 config_cfattach_detach(dm_cd.cd_name, &dm_ca);
178 config_cfdriver_detach(&dm_cd);
186 case MODULE_CMD_FINI:
188 * Disable unloading of dm module if there are any devices
189 * defined in driver. This is probably too strong we need
190 * to disable auto-unload only if there is mounted dm device
193 if (dm_dev_counter > 0)
200 config_cfdriver_detach(&dm_cd);
202 devsw_detach(&dm_bdevsw, &dm_cdevsw);
204 case MODULE_CMD_STAT:
218 * Autoconfiguration match function for pseudo-device glue.
221 dm_match(device_t parent, cfdata_t match,
225 /* Pseudo-device; always present. */
232 * Autoconfiguration attach function for pseudo-device glue.
235 dm_attach(device_t parent, device_t self,
245 * Autoconfiguration detach function for pseudo-device glue.
246 * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
247 * remove devices created in device-mapper.
250 dm_detach(device_t self, int flags)
254 /* Detach device from global device list */
255 if ((dmv = dm_dev_detach(self)) == NULL)
258 /* Destroy active table first. */
259 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
261 /* Destroy inactive table if exits, too. */
262 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
264 dm_table_head_destroy(&dmv->table_head);
266 /* Destroy disk device structure */
267 disk_detach(dmv->diskp);
268 disk_destroy(dmv->diskp);
271 (void)dm_dev_free(dmv);
273 /* Decrement device counter After removing device */
274 atomic_dec_64(&dm_dev_counter);
293 error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
295 aprint_error("%s: unable to register cfattach\n",
303 /* Destroy routine */
309 error = config_cfattach_detach(dm_cd.cd_name, &dm_ca);
322 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
325 aprint_debug("dm open routine called %" PRIu32 "\n", minor(dev));
330 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
333 aprint_debug("dm close routine called %" PRIu32 "\n", minor(dev));
339 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
342 prop_dictionary_t dm_dict_in;
346 aprint_debug("dmioctl called\n");
348 KASSERT(data != NULL);
350 if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
351 struct plistref *pref = (struct plistref *) data;
353 /* Check if we were called with NETBSD_DM_IOCTL ioctl
355 if ((r = dm_ioctl_switch(cmd)) != 0)
358 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
361 if ((r = dm_check_version(dm_dict_in)) != 0)
364 /* run ioctl routine */
365 if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
369 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
370 prop_object_release(dm_dict_in);
377 * Translate command sent from libdevmapper to func.
380 dm_cmd_to_fun(prop_dictionary_t dm_dict){
382 prop_string_t command;
386 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
389 for(i = 0; cmd_fn[i].cmd != NULL; i++)
390 if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
393 if (cmd_fn[i].cmd == NULL)
396 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
397 r = cmd_fn[i].fn(dm_dict);
402 /* Call apropriate ioctl handler function. */
404 dm_ioctl_switch(u_long cmd)
409 case NETBSD_DM_IOCTL:
410 aprint_debug("dm NetBSD_DM_IOCTL called\n");
413 aprint_debug("dm unknown ioctl called\n");
415 break; /* NOT REACHED */
422 * Check for disk specific ioctls.
426 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
430 /* disk ioctls make sense only on block devices */
437 struct dkwedge_info *dkw = (void *) data;
439 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
442 aprint_debug("DIOCGWEDGEINFO ioctl called\n");
444 strlcpy(dkw->dkw_devname, dmv->name, 16);
445 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
446 strlcpy(dkw->dkw_parent, dmv->name, 16);
449 dkw->dkw_size = dm_table_size(&dmv->table_head);
450 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
458 struct plistref *pref = (struct plistref *) data;
460 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
463 if (dmv->diskp->dk_info == NULL) {
467 prop_dictionary_copyout_ioctl(pref, cmd,
468 dmv->diskp->dk_info);
475 aprint_debug("unknown disk_ioctl called\n");
477 break; /* NOT REACHED */
484 * Do all IO operations on dm logical devices.
487 dmstrategy(struct buf *bp)
491 dm_table_entry_t *table_en;
496 uint64_t buf_start, buf_len, issued_len;
497 uint64_t table_start, table_end;
500 buf_start = bp->b_blkno * DEV_BSIZE;
501 buf_len = bp->b_bcount;
509 if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
511 bp->b_resid = bp->b_bcount;
516 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
517 dm_table_size(&dmv->table_head)) <= 0) {
519 bp->b_resid = bp->b_bcount;
525 * disk(9) is part of device structure and it can't be used without
526 * mutual exclusion, use diskp_mtx until it will be fixed.
528 mutex_enter(&dmv->diskp_mtx);
529 disk_busy(dmv->diskp);
530 mutex_exit(&dmv->diskp_mtx);
532 /* Select active table */
533 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
535 /* Nested buffers count down to zero therefore I have
536 to set bp->b_resid to maximal value. */
537 bp->b_resid = bp->b_bcount;
540 * Find out what tables I want to select.
542 SLIST_FOREACH(table_en, tbl, next)
544 /* I need need number of bytes not blocks. */
545 table_start = table_en->start * DEV_BSIZE;
547 * I have to sub 1 from table_en->length to prevent
550 table_end = table_start + (table_en->length)* DEV_BSIZE;
552 start = MAX(table_start, buf_start);
554 end = MIN(table_end, buf_start + buf_len);
556 aprint_debug("----------------------------------------\n");
557 aprint_debug("table_start %010" PRIu64", table_end %010"
558 PRIu64 "\n", table_start, table_end);
559 aprint_debug("buf_start %010" PRIu64", buf_len %010"
560 PRIu64"\n", buf_start, buf_len);
561 aprint_debug("start-buf_start %010"PRIu64", end %010"
562 PRIu64"\n", start - buf_start, end);
563 aprint_debug("start %010" PRIu64" , end %010"
564 PRIu64"\n", start, end);
565 aprint_debug("\n----------------------------------------\n");
568 /* create nested buffer */
569 nestbuf = getiobuf(NULL, true);
571 nestiobuf_setup(bp, nestbuf, start - buf_start,
574 issued_len += end - start;
576 /* I need number of blocks. */
577 nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
579 table_en->target->strategy(table_en, nestbuf);
583 if (issued_len < buf_len)
584 nestiobuf_done(bp, buf_len - issued_len, EINVAL);
586 mutex_enter(&dmv->diskp_mtx);
587 disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0);
588 mutex_exit(&dmv->diskp_mtx);
590 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
598 dmread(dev_t dev, struct uio *uio, int flag)
601 return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
605 dmwrite(dev_t dev, struct uio *uio, int flag)
608 return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
619 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
622 size = dm_table_size(&dmv->table_head);
629 dmminphys(struct buf *bp)
632 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
636 dmgetproperties(struct disk *disk, dm_table_head_t *head)
638 prop_dictionary_t disk_info, odisk_info, geom;
641 dmp_size = dm_table_size(head);
642 disk_info = prop_dictionary_create();
643 geom = prop_dictionary_create();
645 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
646 prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size);
647 prop_dictionary_set_uint32(geom, "sector-size",
648 DEV_BSIZE /* XXX 512? */);
649 prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
650 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
651 prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048);
652 prop_dictionary_set(disk_info, "geometry", geom);
653 prop_object_release(geom);
655 odisk_info = disk->dk_info;
656 disk->dk_info = disk_info;
658 if (odisk_info != NULL)
659 prop_object_release(odisk_info);