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