Home | History | Annotate | Line # | Download | only in dm
dm_target_snapshot.c revision 1.5
      1 /*        $NetBSD: dm_target_snapshot.c,v 1.5 2009/01/02 00:42:31 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  * 1. Suspend my_data to temporarily stop any I/O while the snapshot is being
     34  * activated.
     35  * dmsetup suspend my_data
     36  *
     37  * 2. Create the snapshot-origin device with no table.
     38  * dmsetup create my_data_org
     39  *
     40  * 3. Read the table from my_data and load it into my_data_org.
     41  * dmsetup table my_data | dmsetup load my_data_org
     42  *
     43  * 4. Resume this new table.
     44  * dmsetup resume my_data_org
     45  *
     46  * 5. Create the snapshot device with no table.
     47  * dmsetup create my_data_snap
     48  *
     49  * 6. Load the table into my_data_snap. This uses /dev/hdd1 as the COW device and
     50  * uses a 32kB chunk-size.
     51  * echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot \
     52  *  /dev/mapper/my_data_org /dev/hdd1 p 64" | dmsetup load my_data_snap
     53  *
     54  * 7. Reload my_data as a snapshot-origin device that points to my_data_org.
     55  * echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot-origin \
     56  *  /dev/mapper/my_data_org" | dmsetup load my_data
     57  *
     58  * 8. Resume the snapshot and origin devices.
     59  * dmsetup resume my_data_snap
     60  * dmsetup resume my_data
     61  *
     62  * Before snapshot creation
     63  *  dev_name; dev table
     64  * | my_data; 0 1024 linear /dev/sd1a 384|
     65  *
     66  * After snapshot creation
     67  *                               |my_data_org;0 1024 linear /dev/sd1a 384|
     68  *                              /
     69  * |my_data; 0 1024 snapshot-origin /dev/vg00/my_data_org|
     70  *                           /
     71  * |my_data_snap; 0 1024 snapshot /dev/vg00/my_data /dev/mapper/my_data_cow P 8
     72  *                           \
     73  *                            |my_data_cow; 0 256 linear /dev/sd1a 1408|
     74  */
     75 
     76 /*
     77  * This file implements initial version of device-mapper snapshot target.
     78  */
     79 #include <sys/types.h>
     80 #include <sys/param.h>
     81 
     82 #include <sys/buf.h>
     83 #include <sys/kmem.h>
     84 #include <sys/vnode.h>
     85 
     86 #include "dm.h"
     87 
     88 #ifdef DM_TARGET_MODULE
     89 /*
     90  * Every target can be compiled directly to dm driver or as a
     91  * separate module this part of target is used for loading targets
     92  * to dm driver.
     93  * Target can be unloaded from kernel only if there are no users of
     94  * it e.g. there are no devices which uses that target.
     95  */
     96 #include <sys/kernel.h>
     97 #include <sys/module.h>
     98 
     99 MODULE(MODULE_CLASS_MISC, dm_target_snapshot, NULL);
    100 
    101 static int
    102 dm_target_snapshot_modcmd(modcmd_t cmd, void *arg)
    103 {
    104 	dm_target_t *dmt, *dmt1;
    105 	int r;
    106 	dmt = NULL;
    107 
    108 	switch (cmd) {
    109 	case MODULE_CMD_INIT:
    110 		if (((dmt = dm_target_lookup("snapshot")) != NULL) ||
    111 		    (((dmt = dm_target_lookup("snapshot-origin")) != NULL)))
    112 			return EEXIST;
    113 
    114 		dmt = dm_target_alloc("snapshot");
    115 		dmt1 = dm_target_alloc("snapshot-origin");
    116 
    117 		dmt->version[0] = 1;
    118 		dmt->version[1] = 0;
    119 		dmt->version[2] = 5;
    120 		strlcpy(dmt->name, "snapshot", DM_MAX_TYPE_NAME);
    121 		dmt->init = &dm_target_snapshot_init;
    122 		dmt->status = &dm_target_snapshot_status;
    123 		dmt->strategy = &dm_target_snapshot_strategy;
    124 		dmt->deps = &dm_target_snapshot_deps;
    125 		dmt->destroy = &dm_target_snapshot_destroy;
    126 		dmt->upcall = &dm_target_snapshot_upcall;
    127 
    128 		r = dm_target_insert(dmt);
    129 
    130 		dmt1->version[0] = 1;
    131 		dmt1->version[1] = 0;
    132 		dmt1->version[2] = 5;
    133 		strlcpy(dmt1->name, "snapshot-origin", DM_MAX_TYPE_NAME);
    134 		dmt1->init = &dm_target_snapshot_orig_init;
    135 		dmt1->status = &dm_target_snapshot_orig_status;
    136 		dmt1->strategy = &dm_target_snapshot_orig_strategy;
    137 		dmt1->deps = &dm_target_snapshot_orig_deps;
    138 		dmt1->destroy = &dm_target_snapshot_orig_destroy;
    139 		dmt1->upcall = &dm_target_snapshot_orig_upcall;
    140 
    141 		r = dm_target_insert(dmt1);
    142 		break;
    143 
    144 	case MODULE_CMD_FINI:
    145 		/*
    146 		 * Try to remove snapshot target if it works remove snap-origin
    147 		 * it is not possible to remove snapshot and do not remove
    148 		 * snap-origin because they are used together.
    149 		 */
    150 		if ((r = dm_target_rem("snapshot")) == 0)
    151 			r = dm_target_rem("snapshot-origin");
    152 		break;
    153 
    154 	case MODULE_CMD_STAT:
    155 		return ENOTTY;
    156 
    157 	default:
    158 		return ENOTTY;
    159 	}
    160 
    161 	return r;
    162 }
    163 
    164 #endif
    165 
    166 /*
    167  * Init function called from dm_table_load_ioctl.
    168  * argv:  /dev/mapper/my_data_org /dev/mapper/tsc_cow_dev p 64
    169  *        snapshot_origin device, cow device, persistent flag, chunk size
    170  */
    171 int
    172 dm_target_snapshot_init(dm_dev_t *dmv, void **target_config, char *params)
    173 {
    174 	dm_target_snapshot_config_t *tsc;
    175 	dm_pdev_t *dmp_snap, *dmp_cow;
    176 	char **ap, *argv[5];
    177 
    178 	dmp_cow = NULL;
    179 
    180 	if (params == NULL)
    181 		return EINVAL;
    182 	/*
    183 	 * Parse a string, containing tokens delimited by white space,
    184 	 * into an argument vector
    185 	 */
    186 	for (ap = argv; ap < &argv[4] &&
    187 		 (*ap = strsep(&params, " \t")) != NULL;) {
    188 		if (**ap != '\0')
    189 			ap++;
    190 	}
    191 
    192 	printf("Snapshot target init function called!!\n");
    193 	printf("Snapshotted device: %s, cow device %s,\n\t persistent flag: %s, "
    194 	    "chunk size: %s\n", argv[0], argv[1], argv[2], argv[3]);
    195 
    196 	/* Insert snap device to global pdev list */
    197 	if ((dmp_snap = dm_pdev_insert(argv[0])) == NULL)
    198 		return ENOENT;
    199 
    200 	if ((tsc = kmem_alloc(sizeof(dm_target_snapshot_config_t), KM_NOSLEEP))
    201 	    == NULL)
    202 		return 1;
    203 
    204 	tsc->tsc_persistent_dev = 0;
    205 
    206 	/* There is now cow device for nonpersistent snapshot devices */
    207 	if (strcmp(argv[2], "p") == 0) {
    208 		tsc->tsc_persistent_dev = 1;
    209 
    210 		/* Insert cow device to global pdev list */
    211 		if ((dmp_cow = dm_pdev_insert(argv[1])) == NULL)
    212 			return ENOENT;
    213 	}
    214 
    215 	tsc->tsc_chunk_size = atoi(argv[3]);
    216 
    217 	tsc->tsc_snap_dev = dmp_snap;
    218 	tsc->tsc_cow_dev = dmp_cow;
    219 
    220 	*target_config = tsc;
    221 
    222 	dmv->dev_type = DM_SNAPSHOT_DEV;
    223 
    224 	return 0;
    225 }
    226 
    227 /*
    228  * Status routine is called to get params string, which is target
    229  * specific. When dm_table_status_ioctl is called with flag
    230  * DM_STATUS_TABLE_FLAG I have to sent params string back.
    231  */
    232 char *
    233 dm_target_snapshot_status(void *target_config)
    234 {
    235 	dm_target_snapshot_config_t *tsc;
    236 
    237 	uint32_t i;
    238 	uint32_t count;
    239 	size_t prm_len, cow_len;
    240 	char *params, *cow_name;
    241 
    242 	tsc = target_config;
    243 
    244 	prm_len = 0;
    245 	cow_len = 0;
    246 	count = 0;
    247 	cow_name = NULL;
    248 
    249 	printf("Snapshot target status function called\n");
    250 
    251 	/* count number of chars in offset */
    252 	for(i = tsc->tsc_chunk_size; i != 0; i /= 10)
    253 		count++;
    254 
    255 	if(tsc->tsc_persistent_dev)
    256 		cow_len = strlen(tsc->tsc_cow_dev->name);
    257 
    258 	/* length of names + count of chars + spaces and null char */
    259 	prm_len = strlen(tsc->tsc_snap_dev->name) + cow_len + count + 5;
    260 
    261 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
    262 		return NULL;
    263 
    264 	printf("%s %s %s %"PRIu64"\n", tsc->tsc_snap_dev->name,
    265 		tsc->tsc_cow_dev->name, tsc->tsc_persistent_dev ? "p":"n",
    266 		tsc->tsc_chunk_size);
    267 
    268 	snprintf(params, prm_len, "%s %s %s %"PRIu64, tsc->tsc_snap_dev->name,
    269 		tsc->tsc_persistent_dev ? tsc->tsc_cow_dev->name : "",
    270 		tsc->tsc_persistent_dev ? "p":"n",
    271 		tsc->tsc_chunk_size);
    272 
    273 	return params;
    274 }
    275 
    276 /* Strategy routine called from dm_strategy. */
    277 int
    278 dm_target_snapshot_strategy(dm_table_entry_t *table_en, struct buf *bp)
    279 {
    280 
    281 	printf("Snapshot target read function called!!\n");
    282 
    283 	bp->b_error = EIO;
    284 	bp->b_resid = 0;
    285 
    286 	biodone(bp);
    287 
    288 	return 0;
    289 }
    290 
    291 /* Doesn't do anything here. */
    292 int
    293 dm_target_snapshot_destroy(dm_table_entry_t *table_en)
    294 {
    295 	dm_target_snapshot_config_t *tsc;
    296 
    297 	/*
    298 	 * Destroy function is called for every target even if it
    299 	 * doesn't have target_config.
    300 	 */
    301 
    302 	if (table_en->target_config == NULL)
    303 		return 0;
    304 
    305 	printf("Snapshot target destroy function called\n");
    306 
    307 	tsc = table_en->target_config;
    308 
    309 	/* Decrement pdev ref counter if 0 remove it */
    310 	dm_pdev_decr(tsc->tsc_snap_dev);
    311 
    312 	if (tsc->tsc_persistent_dev)
    313 		dm_pdev_decr(tsc->tsc_cow_dev);
    314 
    315         /* Unbusy target so we can unload it */
    316 	dm_target_unbusy(table_en->target);
    317 
    318 	kmem_free(table_en->target_config, sizeof(dm_target_snapshot_config_t));
    319 
    320 	table_en->target_config = NULL;
    321 
    322 	return 0;
    323 }
    324 
    325 /* Add this target dependiences to prop_array_t */
    326 int
    327 dm_target_snapshot_deps(dm_table_entry_t *table_en,
    328 	prop_array_t prop_array)
    329 {
    330 	dm_target_snapshot_config_t *tsc;
    331 	struct vattr va;
    332 
    333 	int error;
    334 
    335 	if (table_en->target_config == NULL)
    336 		return 0;
    337 
    338 	tsc = table_en->target_config;
    339 
    340 	if ((error = VOP_GETATTR(tsc->tsc_snap_dev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    341 		return error;
    342 
    343 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    344 
    345 	if (tsc->tsc_persistent_dev) {
    346 
    347 		if ((error = VOP_GETATTR(tsc->tsc_cow_dev->pdev_vnode, &va,
    348 				curlwp->l_cred)) != 0)
    349 			return error;
    350 
    351 		prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    352 
    353 	}
    354 
    355 	return 0;
    356 }
    357 
    358 /* Upcall is used to inform other depended devices about IO. */
    359 int
    360 dm_target_snapshot_upcall(dm_table_entry_t *table_en, struct buf *bp)
    361 {
    362 	printf("dm_target_snapshot_upcall called\n");
    363 
    364 	printf("upcall buf flags %s %s\n",
    365 		(bp->b_flags & B_WRITE) ? "B_WRITE":"",
    366 		(bp->b_flags & B_READ) ? "B_READ":"");
    367 
    368 	return 0;
    369 }
    370 
    371 /*
    372  * dm target snapshot origin routines.
    373  *
    374  * Keep for compatibility with linux lvm2tools. They use two targets
    375  * to implement snapshots. Snapshot target will implement exception
    376  * store and snapshot origin will implement device which calls every
    377  * snapshot device when write is done on master device.
    378  */
    379 
    380 /*
    381  * Init function called from dm_table_load_ioctl.
    382  *
    383  * argv: /dev/mapper/my_data_real
    384  */
    385 int
    386 dm_target_snapshot_orig_init(dm_dev_t *dmv, void **target_config,
    387 	char *params)
    388 {
    389 	dm_target_snapshot_origin_config_t *tsoc;
    390 	dm_pdev_t *dmp_real;
    391 
    392 	if (params == NULL)
    393 		return EINVAL;
    394 
    395 	printf("Snapshot origin target init function called!!\n");
    396 	printf("Parent device: %s\n", params);
    397 
    398 	/* Insert snap device to global pdev list */
    399 	if ((dmp_real = dm_pdev_insert(params)) == NULL)
    400 		return ENOENT;
    401 
    402 	if ((tsoc = kmem_alloc(sizeof(dm_target_snapshot_origin_config_t), KM_NOSLEEP))
    403 	    == NULL)
    404 		return 1;
    405 
    406 	tsoc->tsoc_real_dev = dmp_real;
    407 
    408 	dmv->dev_type = DM_SNAPSHOT_ORIG_DEV;
    409 
    410 	*target_config = tsoc;
    411 
    412 	return 0;
    413 }
    414 
    415 /*
    416  * Status routine is called to get params string, which is target
    417  * specific. When dm_table_status_ioctl is called with flag
    418  * DM_STATUS_TABLE_FLAG I have to sent params string back.
    419  */
    420 char *
    421 dm_target_snapshot_orig_status(void *target_config)
    422 {
    423 	dm_target_snapshot_origin_config_t *tsoc;
    424 
    425 	size_t prm_len;
    426 	char *params;
    427 
    428 	tsoc = target_config;
    429 
    430 	prm_len = 0;
    431 
    432 	printf("Snapshot origin target status function called\n");
    433 
    434 	/* length of names + count of chars + spaces and null char */
    435 	prm_len = strlen(tsoc->tsoc_real_dev->name) + 1;
    436 
    437 	printf("real_dev name %s\n",tsoc->tsoc_real_dev->name);
    438 
    439 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
    440 		return NULL;
    441 
    442 	printf("%s\n", tsoc->tsoc_real_dev->name);
    443 
    444 	snprintf(params, prm_len, "%s", tsoc->tsoc_real_dev->name);
    445 
    446 	return params;
    447 }
    448 
    449 /* Strategy routine called from dm_strategy. */
    450 int
    451 dm_target_snapshot_orig_strategy(dm_table_entry_t *table_en, struct buf *bp)
    452 {
    453 
    454 	printf("Snapshot_Orig target read function called!!\n");
    455 
    456 	bp->b_error = EIO;
    457 	bp->b_resid = 0;
    458 
    459 	biodone(bp);
    460 
    461 	return 0;
    462 }
    463 
    464 /* Decrement pdev and free allocated space. */
    465 int
    466 dm_target_snapshot_orig_destroy(dm_table_entry_t *table_en)
    467 {
    468 	dm_target_snapshot_origin_config_t *tsoc;
    469 
    470 	/*
    471 	 * Destroy function is called for every target even if it
    472 	 * doesn't have target_config.
    473 	 */
    474 
    475 	if (table_en->target_config == NULL)
    476 		return 0;
    477 
    478 	tsoc = table_en->target_config;
    479 
    480 	/* Decrement pdev ref counter if 0 remove it */
    481 	dm_pdev_decr(tsoc->tsoc_real_dev);
    482 
    483 	/* Unbusy target so we can unload it */
    484 	dm_target_unbusy(table_en->target);
    485 
    486 	kmem_free(table_en->target_config, sizeof(dm_target_snapshot_origin_config_t));
    487 
    488 	table_en->target_config = NULL;
    489 
    490 	return 0;
    491 }
    492 
    493 /*
    494  * Get target deps and add them to prop_array_t.
    495  */
    496 int
    497 dm_target_snapshot_orig_deps(dm_table_entry_t *table_en,
    498  	prop_array_t prop_array)
    499 {
    500 	dm_target_snapshot_origin_config_t *tsoc;
    501 	struct vattr va;
    502 
    503 	int error;
    504 
    505 	if (table_en->target_config == NULL)
    506 		return 0;
    507 
    508 	tsoc = table_en->target_config;
    509 
    510 	if ((error = VOP_GETATTR(tsoc->tsoc_real_dev->pdev_vnode, &va,
    511 			curlwp->l_cred)) != 0)
    512 		return error;
    513 
    514 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    515 
    516 	return 0;
    517 }
    518 
    519 /* Unsupported for this target. */
    520 int
    521 dm_target_snapshot_orig_upcall(dm_table_entry_t *table_en, struct buf *bp)
    522 {
    523 	return 0;
    524 }
    525