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