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