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