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