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