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