Home | History | Annotate | Line # | Download | only in dm
device-mapper.c revision 1.1.2.14
      1 /*        $NetBSD: device-mapper.c,v 1.1.2.14 2008/10/16 23:26:41 haad Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996, 1997, 1998, 1999, 2002 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/dkio.h>
     42 #include <sys/disk.h>
     43 #include <sys/disklabel.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/ioccom.h>
     46 #include <sys/kmem.h>
     47 #include <sys/module.h>
     48 
     49 #include "netbsd-dm.h"
     50 #include "dm.h"
     51 
     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_dump(dmdump);
     59 static dev_type_size(dmsize);
     60 
     61 /* attach and detach routines */
     62 int dmattach(void);
     63 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 /* ***Variable-definitions*** */
     71 const struct bdevsw dm_bdevsw = {
     72 	dmopen, dmclose, dmstrategy, dmioctl, dmdump, dmsize, D_DISK | D_MPSAFE
     73 };
     74 
     75 const struct cdevsw dm_cdevsw = {
     76 	dmopen, dmclose, dmread, dmwrite, dmioctl,
     77 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK | D_MPSAFE
     78 };
     79 
     80 int unload;
     81 
     82 /*
     83  * This array is used to translate cmd to function pointer.
     84  *
     85  * Interface between libdevmapper and lvm2tools uses different
     86  * names for one IOCTL call because libdevmapper do another thing
     87  * then. When I run "info" or "mknodes" libdevmapper will send same
     88  * ioctl to kernel but will do another things in userspace.
     89  *
     90  */
     91 struct cmd_function cmd_fn[] = {
     92 		{"version", dm_get_version_ioctl},
     93 		{"targets", dm_list_versions_ioctl},
     94 		{"create",  dm_dev_create_ioctl},
     95 		{"info",    dm_dev_status_ioctl},
     96 		{"mknodes", dm_dev_status_ioctl},
     97 		{"names",   dm_dev_list_ioctl},
     98 		{"suspend", dm_dev_suspend_ioctl},
     99 		{"remove",  dm_dev_remove_ioctl},
    100 		{"rename",  dm_dev_rename_ioctl},
    101 		{"resume",  dm_dev_resume_ioctl},
    102 		{"clear",   dm_table_clear_ioctl},
    103 		{"deps",    dm_table_deps_ioctl},
    104 		{"reload",  dm_table_load_ioctl},
    105 		{"status",  dm_table_status_ioctl},
    106 		{"table",   dm_table_status_ioctl},
    107 		{NULL, NULL}
    108 };
    109 
    110 
    111 MODULE(MODULE_CLASS_MISC, dm, NULL);
    112 
    113 /* New module handle routine */
    114 static int
    115 dm_modcmd(modcmd_t cmd, void *arg)
    116 {
    117 #ifdef _MODULE
    118 	int bmajor = -1, cmajor = -1;
    119 
    120 	switch (cmd) {
    121 	case MODULE_CMD_INIT:
    122 		dmattach();
    123 		return devsw_attach("dm", &dm_bdevsw, &bmajor,
    124 		    &dm_cdevsw, &cmajor);
    125 		break;
    126 
    127 	case MODULE_CMD_FINI:
    128 		dmdestroy();
    129 		return devsw_detach(&dm_bdevsw, &dm_cdevsw);
    130 		break;
    131 
    132 	case MODULE_CMD_STAT:
    133 		return ENOTTY;
    134 
    135 	default:
    136 		return ENOTTY;
    137 	}
    138 
    139 	return 0;
    140 #else
    141 
    142 	if (cmd == MODULE_CMD_INIT)
    143 		return 0;
    144 	return ENOTTY;
    145 
    146 #endif /* _MODULE */
    147 }
    148 
    149 
    150 /* attach routine */
    151 int
    152 dmattach(void)
    153 {
    154 	dm_target_init();
    155 	dm_dev_init();
    156 	dm_pdev_init();
    157 
    158 	return 0;
    159 }
    160 
    161 /* Destroy routine */
    162 int
    163 dmdestroy(void)
    164 {
    165 	atomic_inc_32(&unload);
    166 
    167 	dm_dev_destroy();
    168 	dm_pdev_destroy();
    169 	dm_target_destroy();
    170 
    171 	return 0;
    172 }
    173 
    174 static int
    175 dmopen(dev_t dev, int flags, int mode, struct lwp *l)
    176 {
    177 	aprint_debug("open routine called %d\n", minor(dev));
    178 
    179 	return 0;
    180 }
    181 
    182 static int
    183 dmclose(dev_t dev, int flags, int mode, struct lwp *l)
    184 {
    185 	aprint_debug("CLOSE routine called\n");
    186 
    187 	return 0;
    188 }
    189 
    190 
    191 static int
    192 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
    193 {
    194 	int r;
    195 	prop_dictionary_t dm_dict_in;
    196 
    197 	r = 0;
    198 
    199 	aprint_debug("dmioctl called\n");
    200 
    201 	KASSERT(data != NULL);
    202 
    203 	if (disk_ioctl_switch(dev, cmd, data) != 0) {
    204 		struct plistref *pref = (struct plistref *) data;
    205 
    206 		if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0)
    207 			return r;
    208 
    209 		dm_check_version(dm_dict_in);
    210 
    211 		/* call cmd selected function */
    212 		if ((r = dm_ioctl_switch(cmd)) != 0)
    213 			return r;
    214 
    215 		char *xml;
    216 		xml = prop_dictionary_externalize(dm_dict_in);
    217 		aprint_verbose("%s\n",xml);
    218 
    219 		/* run ioctl routine */
    220 		if ((r = dm_cmd_to_fun(dm_dict_in)) != 0)
    221 			return r;
    222 
    223 		r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in);
    224 	}
    225 
    226 	return r;
    227 }
    228 
    229 /*
    230  * Translate command sent from libdevmapper to func.
    231  */
    232 static int
    233 dm_cmd_to_fun(prop_dictionary_t dm_dict){
    234 	int i,len,slen;
    235 	int r;
    236 	const char *command;
    237 
    238 	r = 0;
    239 
    240 	(void)prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_COMMAND,
    241 	    &command);
    242 
    243 	len = strlen(command);
    244 
    245 	for(i=0; cmd_fn[i].cmd != NULL; i++){
    246 		slen = strlen(cmd_fn[i].cmd);
    247 
    248 		if (len != slen)
    249 			continue;
    250 
    251 		if ((strncmp(command, cmd_fn[i].cmd, slen)) == 0) {
    252 			aprint_normal("ioctl command: %s\n", command);
    253 			r = cmd_fn[i].fn(dm_dict);
    254 			break;
    255 		}
    256 	}
    257 
    258 	return r;
    259 }
    260 
    261 /* Call apropriate ioctl handler function. */
    262 static int
    263 dm_ioctl_switch(u_long cmd)
    264 {
    265 	int r;
    266 
    267 	r = 0;
    268 
    269 	switch(cmd) {
    270 
    271 	case NETBSD_DM_IOCTL:
    272 		aprint_debug("NetBSD_DM_IOCTL called\n");
    273 		break;
    274 
    275 	default:
    276 		 aprint_debug("unknown ioctl called\n");
    277 		 return ENOTTY;
    278 		 break; /* NOT REACHED */
    279 	}
    280 
    281 	 return r;
    282 }
    283 
    284  /*
    285   * Check for disk specific ioctls.
    286   */
    287 
    288 static int
    289 disk_ioctl_switch(dev_t dev, u_long cmd, void *data)
    290 {
    291 	struct dm_dev *dmv;
    292 
    293 	switch(cmd) {
    294 	case DIOCGWEDGEINFO:
    295 	{
    296 		struct dkwedge_info *dkw = (void *) data;
    297 
    298 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    299 			return ENOENT;
    300 
    301 		aprint_normal("DIOCGWEDGEINFO ioctl called\n");
    302 
    303 		strlcpy(dkw->dkw_devname, dmv->name, 16);
    304 		strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN);
    305 		strlcpy(dkw->dkw_parent, dmv->name, 16);
    306 
    307 		dkw->dkw_offset = 0;
    308 		dkw->dkw_size = dm_table_size(&dmv->table_head);
    309 		strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
    310 
    311 		dm_dev_unbusy(dmv);
    312 		break;
    313 	}
    314 
    315 	case DIOCGDINFO:
    316 	{
    317 
    318 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    319 			return ENOENT;
    320 
    321 		aprint_debug("DIOCGDINFO %d\n", dmv->dk_label->d_secsize);
    322 
    323 		*(struct disklabel *)data = *(dmv->dk_label);
    324 
    325 		dm_dev_unbusy(dmv);
    326 		break;
    327 	}
    328 
    329 	case DIOCGPART:
    330 	{
    331 		if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    332 			return ENOENT;
    333 
    334 		((struct partinfo *)data)->disklab = dmv->dk_label;
    335 		((struct partinfo *)data)->part = &dmv->dk_label->d_partitions[0];
    336 
    337 		dm_dev_unbusy(dmv);
    338 		break;
    339 	}
    340 	case DIOCWDINFO:
    341 	case DIOCSDINFO:
    342 	case DIOCKLABEL:
    343 	case DIOCWLABEL:
    344 	case DIOCGDEFLABEL:
    345 
    346 	default:
    347 		aprint_debug("unknown disk_ioctl called\n");
    348 		return 1;
    349 		break; /* NOT REACHED */
    350 	}
    351 
    352 	return 0;
    353 }
    354 
    355 /*
    356  * Do all IO operations on dm logical devices.
    357  */
    358 static void
    359 dmstrategy(struct buf *bp)
    360 {
    361 	struct dm_dev *dmv;
    362 	struct dm_table  *tbl;
    363 	struct dm_table_entry *table_en;
    364 	struct buf *nestbuf;
    365 
    366 	uint32_t dev_type;
    367 
    368 	uint64_t buf_start, buf_len, issued_len;
    369 	uint64_t table_start, table_end;
    370 	uint64_t start, end;
    371 
    372 	buf_start = bp->b_blkno * DEV_BSIZE;
    373 	buf_len = bp->b_bcount;
    374 
    375 	tbl = NULL;
    376 
    377 	table_end = 0;
    378 	dev_type = 0;
    379 	issued_len = 0;
    380 
    381 	if (unload == 1){
    382 		bp->b_error = EIO;
    383 		bp->b_resid = bp->b_bcount;
    384 		biodone(bp);
    385 		return;
    386 	}
    387 
    388 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) {
    389 		bp->b_error = EIO;
    390 		bp->b_resid = bp->b_bcount;
    391 		biodone(bp);
    392 		return;
    393 	}
    394 
    395 	/* Select active table */
    396 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    397 
    398 	 /* Nested buffers count down to zero therefore I have
    399 	    to set bp->b_resid to maximal value. */
    400 	bp->b_resid = bp->b_bcount;
    401 
    402 	/*
    403 	 * Find out what tables I want to select.
    404 	 */
    405 	SLIST_FOREACH(table_en, tbl, next)
    406 	{
    407 		/* I need need number of bytes not blocks. */
    408 		table_start = table_en->start * DEV_BSIZE;
    409 		/*
    410 		 * I have to sub 1 from table_en->length to prevent
    411 		 * off by one error
    412 		 */
    413 		table_end = table_start + (table_en->length)* DEV_BSIZE;
    414 
    415 		start = MAX(table_start, buf_start);
    416 
    417 		end = MIN(table_end, buf_start + buf_len);
    418 
    419 		aprint_debug("----------------------------------------\n");
    420 		aprint_debug("table_start %010" PRIu64", table_end %010"
    421 		    PRIu64 "\n", table_start, table_end);
    422 		aprint_debug("buf_start %010" PRIu64", buf_len %010"
    423 		    PRIu64"\n", buf_start, buf_len);
    424 		aprint_debug("start-buf_start %010"PRIu64", end %010"
    425 		    PRIu64"\n", start - buf_start, end);
    426 		aprint_debug("start %010" PRIu64" , end %010"
    427                     PRIu64"\n", start, end);
    428 		aprint_debug("\n----------------------------------------\n");
    429 
    430 		if (start < end) {
    431 			/* create nested buffer  */
    432 			nestbuf = getiobuf(NULL, true);
    433 
    434 			nestiobuf_setup(bp, nestbuf, start - buf_start,
    435 			    (end - start));
    436 
    437 			issued_len += end - start;
    438 
    439 			/* I need number of blocks. */
    440 			nestbuf->b_blkno = (start - table_start) / DEV_BSIZE;
    441 
    442 			table_en->target->strategy(table_en, nestbuf);
    443 		}
    444 	}
    445 
    446 	if (issued_len < buf_len)
    447 		nestiobuf_done(bp, buf_len - issued_len, EINVAL);
    448 
    449 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    450 	dm_dev_unbusy(dmv);
    451 
    452 	return;
    453 }
    454 
    455 
    456 static int
    457 dmread(dev_t dev, struct uio *uio, int flag)
    458 {
    459 	return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio));
    460 }
    461 
    462 static int
    463 dmwrite(dev_t dev, struct uio *uio, int flag)
    464 {
    465 	return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio));
    466 }
    467 
    468 static int
    469 dmdump(dev_t dev, daddr_t blkno, void *va, size_t size)
    470 {
    471 	return ENODEV;
    472 }
    473 
    474 static int
    475 dmsize(dev_t dev)
    476 {
    477 	struct dm_dev *dmv;
    478 	uint64_t size;
    479 
    480 	size = 0;
    481 
    482 	if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL)
    483 			return -ENOENT;
    484 
    485 	size = dm_table_size(&dmv->table_head);
    486 	dm_dev_unbusy(dmv);
    487 
    488   	return size;
    489 }
    490 
    491 static void
    492 dmminphys(struct buf *bp)
    493 {
    494 	bp->b_bcount = MIN(bp->b_bcount, MAXPHYS);
    495 }
    496 
    497  /*
    498   * Load the label information on the named device
    499   * Actually fabricate a disklabel.
    500   *
    501   * EVENTUALLY take information about different
    502   * data tracks from the TOC and put it in the disklabel
    503   *
    504   * Copied from vnd code.
    505   */
    506 void
    507 dmgetdisklabel(struct disklabel *lp, struct dm_table_head *head)
    508 {
    509 	struct partition *pp;
    510 	int dmp_size;
    511 
    512 	dmp_size = dm_table_size(head);
    513 
    514 	/*
    515 	 * Size must be at least 2048 DEV_BSIZE blocks
    516 	 * (1M) in order to use this geometry.
    517 	 */
    518 
    519 	lp->d_secperunit = dmp_size;
    520 	lp->d_secsize = DEV_BSIZE;
    521 	lp->d_nsectors = 32;
    522 	lp->d_ntracks = 64;
    523 	lp->d_ncylinders = dmp_size / (lp->d_nsectors * lp->d_ntracks);
    524 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    525 
    526 	strncpy(lp->d_typename, "lvm", sizeof(lp->d_typename));
    527 	lp->d_type = DTYPE_DM;
    528 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    529 	lp->d_rpm = 3600;
    530 	lp->d_interleave = 1;
    531 	lp->d_flags = 0;
    532 
    533 	pp = &lp->d_partitions[0];
    534 	/*
    535 	 * This is logical offset and therefore it can be 0
    536 	 * I will consider table offsets later in dmstrategy.
    537 	 */
    538 	pp->p_offset = 0;
    539 	pp->p_size = dmp_size * DEV_BSIZE;
    540 	pp->p_fstype = FS_BSDFFS;  /* default value */
    541 	lp->d_npartitions = 1;
    542 
    543 	lp->d_magic = DISKMAGIC;
    544 	lp->d_magic2 = DISKMAGIC;
    545 	lp->d_checksum = dkcksum(lp);
    546 
    547 	return;
    548 }
    549