Home | History | Annotate | Line # | Download | only in dm
device-mapper.c revision 1.16
      1 /*        $NetBSD: device-mapper.c,v 1.16 2010/02/25 20:48:58 jakllsch 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 void dmattach(int);
     63 void dmdestroy(void);
     64 
     65 static void dm_init(void);
     66 static int dm_cmd_to_fun(prop_dictionary_t);
     67 static int disk_ioctl_switch(dev_t, u_long, void *);
     68 static int dm_ioctl_switch(u_long);
     69 static void dmminphys(struct buf *);
     70 
     71 /* CF attach/detach functions used for power management */
     72 static int dm_detach(device_t, int);
     73 static void dm_attach(device_t, device_t, void *);
     74 static int dm_match(device_t, cfdata_t, void *);
     75 
     76 /* ***Variable-definitions*** */
     77 const struct bdevsw dm_bdevsw = {
     78 	.d_open = dmopen,
     79 	.d_close = dmclose,
     80 	.d_strategy = dmstrategy,
     81 	.d_ioctl = dmioctl,
     82 	.d_dump = nodump,
     83 	.d_psize = dmsize,
     84 	.d_flag = D_DISK | D_MPSAFE
     85 };
     86 
     87 const struct cdevsw dm_cdevsw = {
     88 	.d_open = dmopen,
     89 	.d_close = dmclose,
     90 	.d_read = dmread,
     91 	.d_write = dmwrite,
     92 	.d_ioctl = dmioctl,
     93 	.d_stop = nostop,
     94 	.d_tty = notty,
     95 	.d_poll = nopoll,
     96 	.d_mmap = nommap,
     97 	.d_kqfilter = nokqfilter,
     98 	.d_flag = D_DISK | D_MPSAFE
     99 };
    100 
    101 const struct dkdriver dmdkdriver = {
    102 	.d_strategy = dmstrategy
    103 };
    104 
    105 #ifdef _MODULE
    106 /* Autoconf defines */
    107 CFDRIVER_DECL(dm, DV_DISK, NULL);
    108 #endif
    109 
    110 CFATTACH_DECL3_NEW(dm, 0,
    111      dm_match, dm_attach, dm_detach, NULL, NULL, NULL,
    112      DVF_DETACH_SHUTDOWN);
    113 
    114 extern struct cfdriver dm_cd;
    115 
    116 extern uint64_t dm_dev_counter;
    117 
    118 /*
    119  * This array is used to translate cmd to function pointer.
    120  *
    121  * Interface between libdevmapper and lvm2tools uses different
    122  * names for one IOCTL call because libdevmapper do another thing
    123  * then. When I run "info" or "mknodes" libdevmapper will send same
    124  * ioctl to kernel but will do another things in userspace.
    125  *
    126  */
    127 struct cmd_function cmd_fn[] = {
    128 		{ .cmd = "version", .fn = dm_get_version_ioctl},
    129 		{ .cmd = "targets", .fn = dm_list_versions_ioctl},
    130 		{ .cmd = "create",  .fn = dm_dev_create_ioctl},
    131 		{ .cmd = "info",    .fn = dm_dev_status_ioctl},
    132 		{ .cmd = "mknodes", .fn = dm_dev_status_ioctl},
    133 		{ .cmd = "names",   .fn = dm_dev_list_ioctl},
    134 		{ .cmd = "suspend", .fn = dm_dev_suspend_ioctl},
    135 		{ .cmd = "remove",  .fn = dm_dev_remove_ioctl},
    136 		{ .cmd = "rename",  .fn = dm_dev_rename_ioctl},
    137 		{ .cmd = "resume",  .fn = dm_dev_resume_ioctl},
    138 		{ .cmd = "clear",   .fn = dm_table_clear_ioctl},
    139 		{ .cmd = "deps",    .fn = dm_table_deps_ioctl},
    140 		{ .cmd = "reload",  .fn = dm_table_load_ioctl},
    141 		{ .cmd = "status",  .fn = dm_table_status_ioctl},
    142 		{ .cmd = "table",   .fn = dm_table_status_ioctl},
    143 		{NULL, NULL}
    144 };
    145 
    146 MODULE(MODULE_CLASS_DRIVER, dm, NULL);
    147 
    148 /* New module handle routine */
    149 static int
    150 dm_modcmd(modcmd_t cmd, void *arg)
    151 {
    152 #ifdef _MODULE
    153 	int error;
    154 	int bmajor, cmajor;
    155 
    156 	error = 0;
    157 	bmajor = -1;
    158 	cmajor = -1;
    159 
    160 	switch (cmd) {
    161 	case MODULE_CMD_INIT:
    162 		error = config_cfdriver_attach(&dm_cd);
    163 		if (error)
    164 			break;
    165 
    166 		error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
    167 		if (error) {
    168 			config_cfdriver_detach(&dm_cd);
    169 			aprint_error("%s: unable to register cfattach\n",
    170 			    dm_cd.cd_name);
    171 			break;
    172 		}
    173 
    174 		error = devsw_attach(dm_cd.cd_name, &dm_bdevsw, &bmajor,
    175 		    &dm_cdevsw, &cmajor);
    176 		if (error) {
    177 			config_cfattach_detach(dm_cd.cd_name, &dm_ca);
    178 			config_cfdriver_detach(&dm_cd);
    179 			break;
    180 		}
    181 
    182 		dm_init();
    183 
    184 		break;
    185 
    186 	case MODULE_CMD_FINI:
    187 		/*
    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
    191 		 * present.
    192 		 */
    193 		if (dm_dev_counter > 0)
    194 			return EBUSY;
    195 		dmdestroy();
    196 
    197 		error = config_cfattach_detach(dm_cd.cd_name, &dm_ca);
    198 		if (error)
    199 			break;
    200 
    201 		config_cfdriver_detach(&dm_cd);
    202 
    203 		devsw_detach(&dm_bdevsw, &dm_cdevsw);
    204 		break;
    205 	case MODULE_CMD_STAT:
    206 		return ENOTTY;
    207 
    208 	default:
    209 		return ENOTTY;
    210 	}
    211 
    212 	return error;
    213 #else
    214 
    215 	if (cmd == MODULE_CMD_INIT)
    216 		return 0;
    217 	return ENOTTY;
    218 
    219 #endif /* _MODULE */
    220 }
    221 
    222 
    223 /*
    224  * dm_match:
    225  *
    226  *	Autoconfiguration match function for pseudo-device glue.
    227  */
    228 static int
    229 dm_match(device_t parent, cfdata_t match,
    230     void *aux)
    231 {
    232 
    233 	/* Pseudo-device; always present. */
    234 	return (1);
    235 }
    236 
    237 /*
    238  * dm_attach:
    239  *
    240  *	Autoconfiguration attach function for pseudo-device glue.
    241  */
    242 static void
    243 dm_attach(device_t parent, device_t self,
    244     void *aux)
    245 {
    246 	return;
    247 }
    248 
    249 
    250 /*
    251  * dm_detach:
    252  *
    253  *	Autoconfiguration detach function for pseudo-device glue.
    254  * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to
    255  * remove devices created in device-mapper.
    256  */
    257 static int
    258 dm_detach(device_t self, int flags)
    259 {
    260 	dm_dev_t *dmv;
    261 
    262 	/* Detach device from global device list */
    263 	if ((dmv = dm_dev_detach(self)) == NULL)
    264 		return ENOENT;
    265 
    266 	/* Destroy active table first.  */
    267 	dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
    268 
    269 	/* Destroy inactive table if exits, too. */
    270 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    271 
    272 	dm_table_head_destroy(&dmv->table_head);
    273 
    274 	/* Destroy disk device structure */
    275 	disk_detach(dmv->diskp);
    276 	disk_destroy(dmv->diskp);
    277 
    278 	/* Destroy device */
    279 	(void)dm_dev_free(dmv);
    280 
    281 	/* Decrement device counter After removing device */
    282 	atomic_dec_64(&dm_dev_counter);
    283 
    284 	return 0;
    285 }
    286 
    287 /* attach routine */
    288 void
    289 dmattach(int num)
    290 {
    291 	int error;
    292 
    293 	error = config_cfattach_attach(dm_cd.cd_name, &dm_ca);
    294 	if (error) {
    295 		aprint_error("%s: unable to register cfattach\n",
    296 		    dm_cd.cd_name);
    297 		return;
    298 	}
    299 
    300 	dm_init();
    301 }
    302 
    303 /* Destroy routine */
    304 void
    305 dmdestroy(void)
    306 {
    307 
    308 	dm_dev_destroy();
    309 	dm_pdev_destroy();
    310 	dm_target_destroy();
    311 
    312 }
    313 
    314 static int
    315 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
    316 {
    317 
    318 	aprint_debug("dm open routine called %" PRIu32 "\n", minor(dev));
    319 	return 0;
    320 }
    321 
    322 static int
    323 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
    324 {
    325 
    326 	aprint_debug("dm close routine called %" PRIu32 "\n", minor(dev));
    327 	return 0;
    328 }
    329 
    330 
    331 static int
    332 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
    333 {
    334 	int r;
    335 	prop_dictionary_t dm_dict_in;
    336 
    337 	r = 0;
    338 
    339 	aprint_debug("dmioctl called\n");
    340 
    341 	KASSERT(data != NULL);
    342 
    343 	if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) {
    344 		struct plistref *pref = (struct plistref *) data;
    345 
    346 		/* Check if we were called with NETBSD_DM_IOCTL ioctl
    347 		   otherwise quit. */
    348 		if ((r = dm_ioctl_switch(cmd)) != 0)
    349 			return r;
    350 
    351 		if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
    352 			return r;
    353 
    354 		if ((r = dm_check_version(dm_dict_in)) != 0)
    355 			goto cleanup_exit;
    356 
    357 		/* run ioctl routine */
    358 		if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
    359 			goto cleanup_exit;
    360 
    361 cleanup_exit:
    362 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
    363 		prop_object_release(dm_dict_in);
    364 	}
    365 
    366 	return r;
    367 }
    368 
    369 static void
    370 dm_init(void)
    371 {
    372 	dm_target_init();
    373 	dm_dev_init();
    374 	dm_pdev_init();
    375 }
    376 
    377 /*
    378  * Translate command sent from libdevmapper to func.
    379  */
    380 static int
    381 dm_cmd_to_fun(prop_dictionary_t dm_dict){
    382 	int i, r;
    383 	prop_string_t command;
    384 
    385 	r = 0;
    386 
    387 	if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL)
    388 		return EINVAL;
    389 
    390 	for(i = 0; cmd_fn[i].cmd != NULL; i++)
    391 		if (prop_string_equals_cstring(command, cmd_fn[i].cmd))
    392 			break;
    393 
    394 	if (cmd_fn[i].cmd == NULL)
    395 		return EINVAL;
    396 
    397 	aprint_debug("ioctl %s called\n", cmd_fn[i].cmd);
    398 	r = cmd_fn[i].fn(dm_dict);
    399 
    400 	return r;
    401 }
    402 
    403 /* Call apropriate ioctl handler function. */
    404 static int
    405 dm_ioctl_switch(u_long cmd)
    406 {
    407 
    408 	switch(cmd) {
    409 
    410 	case NETBSD_DM_IOCTL:
    411 		aprint_debug("dm NetBSD_DM_IOCTL called\n");
    412 		break;
    413 	default:
    414 		 aprint_debug("dm unknown ioctl called\n");
    415 		 return ENOTTY;
    416 		 break; /* NOT REACHED */
    417 	}
    418 
    419 	 return 0;
    420 }
    421 
    422  /*
    423   * Check for disk specific ioctls.
    424   */
    425 
    426 static int
    427 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
    428 {
    429 	dm_dev_t *dmv;
    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