Home | History | Annotate | Line # | Download | only in dm
device-mapper.c revision 1.20
      1 /*        $NetBSD: device-mapper.c,v 1.20 2010/03/12 16:26:26 haad Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Hamsik.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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.
     18  *
     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.
     30  */
     31 
     32 /*
     33  * I want to say thank you to all people who helped me with this project.
     34  */
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 
     39 #include <sys/buf.h>
     40 #include <sys/conf.h>
     41 #include <sys/device.h>
     42 #include <sys/dkio.h>
     43 #include <sys/disk.h>
     44 #include <sys/disklabel.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/ioccom.h>
     47 #include <sys/kmem.h>
     48 #include <sys/module.h>
     49 #include <sys/once.h>
     50 
     51 #include "netbsd-dm.h"
     52 #include "dm.h"
     53 
     54 static dev_type_open(dmopen);
     55 static dev_type_close(dmclose);
     56 static dev_type_read(dmread);
     57 static dev_type_write(dmwrite);
     58 static dev_type_ioctl(dmioctl);
     59 static dev_type_strategy(dmstrategy);
     60 static dev_type_size(dmsize);
     61 
     62 /* attach and detach routines */
     63 void dmattach(int);
     64 int dmdestroy(void);
     65 
     66 static ONCE_DECL(doinit_control);
     67 static int doinit(void);
     68 
     69 static int dm_cmd_to_fun(prop_dictionary_t);
     70 static int disk_ioctl_switch(dev_t, u_long, void *);
     71 static int dm_ioctl_switch(u_long);
     72 static void dmminphys(struct buf *);
     73 
     74 /* CF attach/detach functions used for power management */
     75 static int dm_detach(device_t, int);
     76 static void dm_attach(device_t, device_t, void *);
     77 static int dm_match(device_t, cfdata_t, void *);
     78 
     79 /* ***Variable-definitions*** */
     80 const struct bdevsw dm_bdevsw = {
     81 	.d_open = dmopen,
     82 	.d_close = dmclose,
     83 	.d_strategy = dmstrategy,
     84 	.d_ioctl = dmioctl,
     85 	.d_dump = nodump,
     86 	.d_psize = dmsize,
     87 	.d_flag = D_DISK | D_MPSAFE
     88 };
     89 
     90 const struct cdevsw dm_cdevsw = {
     91 	.d_open = dmopen,
     92 	.d_close = dmclose,
     93 	.d_read = dmread,
     94 	.d_write = dmwrite,
     95 	.d_ioctl = dmioctl,
     96 	.d_stop = nostop,
     97 	.d_tty = notty,
     98 	.d_poll = nopoll,
     99 	.d_mmap = nommap,
    100 	.d_kqfilter = nokqfilter,
    101 	.d_flag = D_DISK | D_MPSAFE
    102 };
    103 
    104 const struct dkdriver dmdkdriver = {
    105 	.d_strategy = dmstrategy
    106 };
    107 
    108 #ifdef _MODULE
    109 /* Autoconf defines */
    110 CFDRIVER_DECL(dm, DV_DISK, NULL);
    111 #endif
    112 
    113 CFATTACH_DECL3_NEW(dm, 0,
    114      dm_match, dm_attach, dm_detach, NULL, NULL, NULL,
    115      DVF_DETACH_SHUTDOWN);
    116 
    117 extern struct cfdriver dm_cd;
    118 
    119 extern uint64_t dm_dev_counter;
    120 
    121 /*
    122  * This array is used to translate cmd to function pointer.
    123  *
    124  * Interface between libdevmapper and lvm2tools uses different
    125  * names for one IOCTL call because libdevmapper do another thing
    126  * then. When I run "info" or "mknodes" libdevmapper will send same
    127  * ioctl to kernel but will do another things in userspace.
    128  *
    129  */
    130 struct cmd_function cmd_fn[] = {
    131 		{ .cmd = "version", .fn = dm_get_version_ioctl},
    132 		{ .cmd = "targets", .fn = dm_list_versions_ioctl},
    133 		{ .cmd = "create",  .fn = dm_dev_create_ioctl},
    134 		{ .cmd = "info",    .fn = dm_dev_status_ioctl},
    135 		{ .cmd = "mknodes", .fn = dm_dev_status_ioctl},
    136 		{ .cmd = "names",   .fn = dm_dev_list_ioctl},
    137 		{ .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
    138 		{ .cmd = "remove",  .fn = dm_dev_remove_ioctl},
    139 		{ .cmd = "rename",  .fn = dm_dev_rename_ioctl},
    140 		{ .cmd = "resume",  .fn = dm_dev_resume_ioctl},
    141 		{ .cmd = "clear",   .fn = dm_table_clear_ioctl},
    142 		{ .cmd = "deps",    .fn = dm_table_deps_ioctl},
    143 		{ .cmd = "reload",  .fn = dm_table_load_ioctl},
    144 		{ .cmd = "status",  .fn = dm_table_status_ioctl},
    145 		{ .cmd = "table",   .fn = dm_table_status_ioctl},
    146 		{NULL, NULL}
    147 };
    148 
    149 MODULE(MODULE_CLASS_DRIVER, dm, NULL);
    150 
    151 /* New module handle routine */
    152 static int
    153 dm_modcmd(modcmd_t cmd, void *arg)
    154 {
    155 	int error, bmajor, cmajor;
    156 
    157 	error = 0;
    158 	bmajor = -1;
    159 	cmajor = -1;
    160 
    161 	switch (cmd) {
    162 	case MODULE_CMD_INIT:
    163 		error = config_cfdriver_attach(&dm_cd);
    164 		if (error)
    165 			break;
    166 
    167 		error = RUN_ONCE(&doinit_control, doinit);
    168 		if (error) {
    169 			config_cfdriver_detach(&dm_cd);
    170 			break;
    171 		}
    172 
    173 		error = devsw_attach(dm_cd.cd_name, &dm_bdevsw, &bmajor,
    174 		    &dm_cdevsw, &cmajor);
    175 		if (error) {
    176 			config_cfattach_detach(dm_cd.cd_name, &dm_ca);
    177 			config_cfdriver_detach(&dm_cd);
    178 			break;
    179 		}
    180 		break;
    181 
    182 	case MODULE_CMD_FINI:
    183 		/*
    184 		 * Disable unloading of dm module if there are any devices
    185 		 * defined in driver. This is probably too strong we need
    186 		 * to disable auto-unload only if there is mounted dm device
    187 		 * present.
    188 		 */
    189 		if (dm_dev_counter > 0)
    190 			return EBUSY;
    191 
    192 		error = dmdestroy();
    193 		if (error)
    194 			break;
    195 
    196 		config_cfdriver_detach(&dm_cd);
    197 
    198 		devsw_detach(&dm_bdevsw, &dm_cdevsw);
    199 		break;
    200 	case MODULE_CMD_STAT:
    201 		return ENOTTY;
    202 
    203 	default:
    204 		return ENOTTY;
    205 	}
    206 
    207 	return error;
    208 }
    209 
    210 
    211 /*
    212  * dm_match:
    213  *
    214  *	Autoconfiguration match function for pseudo-device glue.
    215  */
    216 static int
    217 dm_match(device_t parent, cfdata_t match,
    218     void *aux)
    219 {
    220 
    221 	/* Pseudo-device; always present. */
    222 	return (1);
    223 }
    224 
    225 /*
    226  * dm_attach:
    227  *
    228  *	Autoconfiguration attach function for pseudo-device glue.
    229  */
    230 static void
    231 dm_attach(device_t parent, device_t self,
    232     void *aux)
    233 {
    234 	return;
    235 }
    236 
    237 
    238 /*
    239  * dm_detach:
    240  *
    241  *	Autoconfiguration detach function for pseudo-device glue.
    242  * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
    243  * remove devices created in device-mapper.
    244  */
    245 static int
    246 dm_detach(device_t self, int flags)
    247 {
    248 	dm_dev_t *dmv;
    249 
    250 	/* Detach device from global device list */
    251 	if ((dmv = dm_dev_detach(self)) == NULL)
    252 		return ENOENT;
    253 
    254 	/* Destroy active table first.  */
    255 	dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
    256 
    257 	/* Destroy inactive table if exits, too. */
    258 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    259 
    260 	dm_table_head_destroy(&dmv->table_head);
    261 
    262 	/* Destroy disk device structure */
    263 	disk_detach(dmv->diskp);
    264 	disk_destroy(dmv->diskp);
    265 
    266 	/* Destroy device */
    267 	(void)dm_dev_free(dmv);
    268 
    269 	/* Decrement device counter After removing device */
    270 	atomic_dec_64(&dm_dev_counter);
    271 
    272 	return 0;
    273 }
    274 
    275 static int
    276 doinit(void)
    277 {
    278 	int error;
    279 
    280 	error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
    281 	if (error) {
    282 		aprint_error("%s: unable to register cfattach\n",
    283 		    dm_cd.cd_name);
    284 		return error;
    285 	}
    286 
    287 	dm_target_init();
    288 	dm_dev_init();
    289 	dm_pdev_init();
    290 
    291 	return 0;
    292 }
    293 
    294 /* attach routine */
    295 void
    296 dmattach(int n)
    297 {
    298 	RUN_ONCE(&doinit_control, doinit);
    299 }
    300 
    301 /* Destroy routine */
    302 int
    303 dmdestroy(void)
    304 {
    305 	int error;
    306 
    307 	error = config_cfattach_detach(dm_cd.cd_name, &dm_ca);
    308 	if (error)
    309 		return error;
    310 
    311 	dm_dev_destroy();
    312 	dm_pdev_destroy();
    313 	dm_target_destroy();
    314 
    315 	return 0;
    316 }
    317 
    318 static int
    319 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
    320 {
    321 
    322 	aprint_debug("dm open routine called %" PRIu32 "\n", minor(dev));
    323 	return 0;
    324 }
    325 
    326 static int
    327 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
    328 {
    329 
    330 	aprint_debug("dm close routine called %" PRIu32 "\n", minor(dev));
    331 	return 0;
    332 }
    333 
    334 
    335 static int
    336 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
    337 {
    338 	int r;
    339 	prop_dictionary_t dm_dict_in;
    340 
    341 	r = 0;
    342 
    343 	aprint_debug("dmioctl called\n");
    344 
    345 	KASSERT(data != NULL);
    346 
    347 	if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
    348 		struct plistref *pref = (struct plistref *) data;
    349 
    350 		/* Check if we were called with NETBSD_DM_IOCTL ioctl
    351 		   otherwise quit. */
    352 		if ((r = dm_ioctl_switch(cmd)) != 0)
    353 			return r;
    354 
    355 		if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
    356 			return r;
    357 
    358 		if ((r = dm_check_version(dm_dict_in)) != 0)
    359 			goto cleanup_exit;
    360 
    361 		/* run ioctl routine */
    362 		if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
    363 			goto cleanup_exit;
    364 
    365 cleanup_exit:
    366 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
    367 		prop_object_release(dm_dict_in);
    368 	}
    369 
    370 	return r;
    371 }
    372 
    373 /*
    374  * Translate command sent from libdevmapper to func.
    375  */
    376 static int
    377 dm_cmd_to_fun(prop_dictionary_t dm_dict){
    378 	int i, r;
    379 	prop_string_t command;
    380 
    381 	r = 0;
    382 
    383 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
    384 		return EINVAL;
    385 
    386 	for(i = 0; cmd_fn[i].cmd != NULL; i++)
    387 		if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
    388 			break;
    389 
    390 	if (cmd_fn[i].cmd == NULL)
    391 		return EINVAL;
    392 
    393 	aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
    394 	r = cmd_fn[i].fn(dm_dict);
    395 
    396 	return r;
    397 }
    398 
    399 /* Call apropriate ioctl handler function. */
    400 static int
    401 dm_ioctl_switch(u_long cmd)
    402 {
    403 
    404 	switch(cmd) {
    405 
    406 	case NETBSD_DM_IOCTL:
    407 		aprint_debug("dm NetBSD_DM_IOCTL called\n");
    408 		break;
    409 	default:
    410 		 aprint_debug("dm unknown ioctl called\n");
    411 		 return ENOTTY;
    412 		 break; /* NOT REACHED */
    413 	}
    414 
    415 	 return 0;
    416 }
    417 
    418  /*
    419   * Check for disk specific ioctls.
    420   */
    421 
    422 static int
    423 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
    424 {
    425 	dm_dev_t *dmv;
    426 
    427 	/* disk ioctls make sense only on block devices */
    428 	if (minor(dev) == 0)
    429 		return ENOTTY;
    430 
    431 	switch(cmd) {
    432 	case DIOCGWEDGEINFO:
    433 	{
    434 		struct dkwedge_info *dkw = (void *) data;
    435 
    436 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    437 			return ENODEV;
    438 
    439 		aprint_debug("DIOCGWEDGEINFO ioctl called\n");
    440 
    441 		strlcpy(dkw->dkw_devname, dmv->name, 16);
    442 		strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
    443 		strlcpy(dkw->dkw_parent, dmv->name, 16);
    444 
    445 		dkw->dkw_offset = 0;
    446 		dkw->dkw_size = dm_table_size(&dmv->table_head);
    447 		strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
    448 
    449 		dm_dev_unbusy(dmv);
    450 		break;
    451 	}
    452 
    453 	case DIOCGDISKINFO:
    454 	{
    455 		struct plistref *pref = (struct plistref *) data;
    456 
    457 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    458 			return ENODEV;
    459 
    460 		if (dmv->diskp->dk_info == NULL) {
    461 			dm_dev_unbusy(dmv);
    462 			return ENOTSUP;
    463 		} else
    464 			prop_dictionary_copyout_ioctl(pref, cmd,
    465 			    dmv->diskp->dk_info);
    466 
    467 		dm_dev_unbusy(dmv);
    468 		break;
    469 	}
    470 
    471 	default:
    472 		aprint_debug("unknown disk_ioctl called\n");
    473 		return ENOTTY;
    474 		break; /* NOT REACHED */
    475 	}
    476 
    477 	return 0;
    478 }
    479 
    480 /*
    481  * Do all IO operations on dm logical devices.
    482  */
    483 static void
    484 dmstrategy(struct buf *bp)
    485 {
    486 	dm_dev_t *dmv;
    487 	dm_table_t  *tbl;
    488 	dm_table_entry_t *table_en;
    489 	struct buf *nestbuf;
    490 
    491 	uint32_t dev_type;
    492 
    493 	uint64_t buf_start, buf_len, issued_len;
    494 	uint64_t table_start, table_end;
    495 	uint64_t start, end;
    496 
    497 	buf_start = bp->b_blkno * DEV_BSIZE;
    498 	buf_len = bp->b_bcount;
    499 
    500 	tbl = NULL;
    501 
    502 	table_end = 0;
    503 	dev_type = 0;
    504 	issued_len = 0;
    505 
    506 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
    507 		bp->b_error = EIO;
    508 		bp->b_resid = bp->b_bcount;
    509 		biodone(bp);
    510 		return;
    511 	}
    512 
    513 	if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    514 	    dm_table_size(&dmv->table_head)) <= 0) {
    515 		dm_dev_unbusy(dmv);
    516 		bp->b_resid = bp->b_bcount;
    517 		biodone(bp);
    518 		return;
    519 	}
    520 
    521 	/*
    522 	 * disk(9) is part of device structure and it can't be used without
    523 	 * mutual exclusion, use diskp_mtx until it will be fixed.
    524 	 */
    525 	mutex_enter(&dmv->diskp_mtx);
    526 	disk_busy(dmv->diskp);
    527 	mutex_exit(&dmv->diskp_mtx);
    528 
    529 	/* Select active table */
    530 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    531 
    532 	 /* Nested buffers count down to zero therefore I have
    533 	    to set bp->b_resid to maximal value. */
    534 	bp->b_resid = bp->b_bcount;
    535 
    536 	/*
    537 	 * Find out what tables I want to select.
    538 	 */
    539 	SLIST_FOREACH(table_en, tbl, next)
    540 	{
    541 		/* I need need number of bytes not blocks. */
    542 		table_start = table_en->start * DEV_BSIZE;
    543 		/*
    544 		 * I have to sub 1 from table_en->length to prevent
    545 		 * off by one error
    546 		 */
    547 		table_end = table_start + (table_en->length)* DEV_BSIZE;
    548 
    549 		start = MAX(table_start, buf_start);
    550 
    551 		end = MIN(table_end, buf_start + buf_len);
    552 
    553 		aprint_debug("----------------------------------------\n");
    554 		aprint_debug("table_start %010" PRIu64", table_end %010"
    555 		    PRIu64 "\n", table_start, table_end);
    556 		aprint_debug("buf_start %010" PRIu64", buf_len %010"
    557 		    PRIu64"\n", buf_start, buf_len);
    558 		aprint_debug("start-buf_start %010"PRIu64", end %010"
    559 		    PRIu64"\n", start - buf_start, end);
    560 		aprint_debug("start %010" PRIu64" , end %010"
    561                     PRIu64"\n", start, end);
    562 		aprint_debug("\n----------------------------------------\n");
    563 
    564 		if (start < end) {
    565 			/* create nested buffer  */
    566 			nestbuf = getiobuf(NULL, true);
    567 
    568 			nestiobuf_setup(bp, nestbuf, start - buf_start,
    569 			    (end - start));
    570 
    571 			issued_len += end - start;
    572 
    573 			/* I need number of blocks. */
    574 			nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
    575 
    576 			table_en->target->strategy(table_en, nestbuf);
    577 		}
    578 	}
    579 
    580 	if (issued_len < buf_len)
    581 		nestiobuf_done(bp, buf_len - issued_len, EINVAL);
    582 
    583 	mutex_enter(&dmv->diskp_mtx);
    584 	disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0);
    585 	mutex_exit(&dmv->diskp_mtx);
    586 
    587 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    588 	dm_dev_unbusy(dmv);
    589 
    590 	return;
    591 }
    592 
    593 
    594 static int
    595 dmread(dev_t dev, struct uio *uio, int flag)
    596 {
    597 
    598 	return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
    599 }
    600 
    601 static int
    602 dmwrite(dev_t dev, struct uio *uio, int flag)
    603 {
    604 
    605 	return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
    606 }
    607 
    608 static int
    609 dmsize(dev_t dev)
    610 {
    611 	dm_dev_t *dmv;
    612 	uint64_t size;
    613 
    614 	size = 0;
    615 
    616 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    617 			return -ENOENT;
    618 
    619 	size = dm_table_size(&dmv->table_head);
    620 	dm_dev_unbusy(dmv);
    621 
    622   	return size;
    623 }
    624 
    625 static void
    626 dmminphys(struct buf *bp)
    627 {
    628 
    629 	bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
    630 }
    631 
    632 void
    633 dmgetproperties(struct disk *disk, dm_table_head_t *head)
    634 {
    635 	prop_dictionary_t disk_info, odisk_info, geom;
    636 	int dmp_size;
    637 
    638 	dmp_size = dm_table_size(head);
    639 	disk_info = prop_dictionary_create();
    640 	geom = prop_dictionary_create();
    641 
    642 	prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
    643 	prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size);
    644 	prop_dictionary_set_uint32(geom, "sector-size",
    645 	    DEV_BSIZE /* XXX 512? */);
    646 	prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
    647 	prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
    648 	prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048);
    649 	prop_dictionary_set(disk_info, "geometry", geom);
    650 	prop_object_release(geom);
    651 
    652 	odisk_info = disk->dk_info;
    653 	disk->dk_info = disk_info;
    654 
    655 	if (odisk_info != NULL)
    656 		prop_object_release(odisk_info);
    657 }
    658