1 /* $NetBSD: dm_target_snapshot.c,v 1.12 2010/01/04 00:12:22 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/kmem.h> 80 81 #include <dev/disk/dm/dm.h> 82 83 typedef struct target_snapshot_config { 84 dm_pdev_t *tsc_snap_dev; 85 /* cow dev is set only for persistent snapshot devices */ 86 dm_pdev_t *tsc_cow_dev; 87 88 uint64_t tsc_chunk_size; 89 uint32_t tsc_persistent_dev; 90 } dm_target_snapshot_config_t; 91 92 typedef struct target_snapshot_origin_config { 93 dm_pdev_t *tsoc_real_dev; 94 /* list of snapshots ? */ 95 } dm_target_snapshot_origin_config_t; 96 97 int dm_target_snapshot_init(dm_table_entry_t *, int, char **); 98 char *dm_target_snapshot_table(void *); 99 int dm_target_snapshot_strategy(dm_table_entry_t *, struct buf *); 100 int dm_target_snapshot_destroy(dm_table_entry_t *); 101 102 int dm_target_snapshot_orig_init(dm_table_entry_t *, int, char **); 103 char *dm_target_snapshot_orig_table(void *); 104 int dm_target_snapshot_orig_strategy(dm_table_entry_t *, struct buf *); 105 int dm_target_snapshot_orig_destroy(dm_table_entry_t *); 106 107 /* 108 * Init function called from dm_table_load_ioctl. 109 * argv: /dev/mapper/my_data_org /dev/mapper/tsc_cow_dev p 64 110 * snapshot_origin device, cow device, persistent flag, chunk size 111 */ 112 int 113 dm_target_snapshot_init(dm_table_entry_t *table_en, int argc, char **argv) 114 { 115 dm_target_snapshot_config_t *tsc; 116 dm_pdev_t *dmp_snap, *dmp_cow; 117 118 dmp_cow = NULL; 119 120 kprintf("Snapshot target init function called!!\n"); 121 kprintf("Snapshotted device: %s, cow device %s,\n\t persistent flag: %s, " 122 "chunk size: %s\n", argv[0], argv[1], argv[2], argv[3]); 123 124 /* Insert snap device to global pdev list */ 125 if ((dmp_snap = dm_pdev_insert(argv[0])) == NULL) 126 return ENOENT; 127 128 tsc = kmem_alloc(sizeof(dm_target_snapshot_config_t), 129 VM_SUBSYS_DM, KM_NOSLEEP); 130 if (tsc == NULL) 131 return 1; 132 133 tsc->tsc_persistent_dev = 0; 134 135 /* There is now cow device for nonpersistent snapshot devices */ 136 if (strcmp(argv[2], "p") == 0) { 137 tsc->tsc_persistent_dev = 1; 138 139 /* Insert cow device to global pdev list */ 140 if ((dmp_cow = dm_pdev_insert(argv[1])) == NULL) 141 return ENOENT; 142 } 143 tsc->tsc_chunk_size = atoi64(argv[3]); 144 145 tsc->tsc_snap_dev = dmp_snap; 146 tsc->tsc_cow_dev = dmp_cow; 147 148 dm_table_add_deps(table_en, dmp_snap); 149 dm_table_add_deps(table_en, dmp_cow); 150 151 dm_table_init_target(table_en, tsc); 152 153 return 0; 154 } 155 156 /* 157 * Table routine is called to get params string, which is target 158 * specific. When dm_table_status_ioctl is called with flag 159 * DM_STATUS_TABLE_FLAG I have to sent params string back. 160 */ 161 char * 162 dm_target_snapshot_table(void *target_config) 163 { 164 dm_target_snapshot_config_t *tsc; 165 166 uint32_t i; 167 uint32_t count; 168 size_t prm_len, cow_len; 169 char *params, *cow_name; 170 171 tsc = target_config; 172 173 prm_len = 0; 174 cow_len = 0; 175 count = 0; 176 cow_name = NULL; 177 178 kprintf("Snapshot target table function called\n"); 179 180 /* count number of chars in offset */ 181 for (i = tsc->tsc_chunk_size; i != 0; i /= 10) 182 count++; 183 184 if (tsc->tsc_persistent_dev) 185 cow_len = strlen(tsc->tsc_cow_dev->name); 186 187 /* length of names + count of chars + spaces and null char */ 188 prm_len = strlen(tsc->tsc_snap_dev->name) + cow_len + count + 5; 189 190 params = dm_alloc_string(prm_len); 191 192 kprintf("%s %s %s %" PRIu64 "\n", tsc->tsc_snap_dev->name, 193 tsc->tsc_cow_dev->name, tsc->tsc_persistent_dev ? "p" : "n", 194 tsc->tsc_chunk_size); 195 196 ksnprintf(params, prm_len, "%s %s %s %" PRIu64, tsc->tsc_snap_dev->name, 197 tsc->tsc_persistent_dev ? tsc->tsc_cow_dev->name : "", 198 tsc->tsc_persistent_dev ? "p" : "n", 199 tsc->tsc_chunk_size); 200 201 return params; 202 } 203 204 /* Strategy routine called from dm_strategy. */ 205 int 206 dm_target_snapshot_strategy(dm_table_entry_t *table_en, struct buf *bp) 207 { 208 209 kprintf("Snapshot target read function called!!\n"); 210 211 bp->b_error = EIO; 212 bp->b_resid = 0; 213 214 biodone(bp); 215 216 return 0; 217 } 218 219 /* Doesn't do anything here. */ 220 int 221 dm_target_snapshot_destroy(dm_table_entry_t *table_en) 222 { 223 dm_target_snapshot_config_t *tsc; 224 225 /* 226 * Destroy function is called for every target even if it 227 * doesn't have target_config. 228 */ 229 230 if (table_en->target_config == NULL) 231 return 0; 232 233 kprintf("Snapshot target destroy function called\n"); 234 235 tsc = table_en->target_config; 236 237 /* Decrement pdev ref counter if 0 remove it */ 238 dm_pdev_decr(tsc->tsc_snap_dev); 239 240 if (tsc->tsc_persistent_dev) 241 dm_pdev_decr(tsc->tsc_cow_dev); 242 243 kmem_free(table_en->target_config, sizeof(dm_target_snapshot_config_t)); 244 245 return 0; 246 } 247 248 /* 249 * dm target snapshot origin routines. 250 * 251 * Keep for compatibility with linux lvm2tools. They use two targets 252 * to implement snapshots. Snapshot target will implement exception 253 * store and snapshot origin will implement device which calls every 254 * snapshot device when write is done on master device. 255 */ 256 257 /* 258 * Init function called from dm_table_load_ioctl. 259 * 260 * argv: /dev/mapper/my_data_real 261 */ 262 int 263 dm_target_snapshot_orig_init(dm_table_entry_t *table_en, int argc, char **argv) 264 { 265 dm_target_snapshot_origin_config_t *tsoc; 266 dm_pdev_t *dmp_real; 267 268 kprintf("Snapshot origin target init function called!!\n"); 269 kprintf("Parent device: %s\n", argv[0]); 270 271 /* Insert snap device to global pdev list */ 272 if ((dmp_real = dm_pdev_insert(argv[0])) == NULL) 273 return ENOENT; 274 275 tsoc = kmem_alloc(sizeof(dm_target_snapshot_origin_config_t), 276 VM_SUBSYS_DM, KM_NOSLEEP); 277 if (tsoc == NULL) 278 return 1; 279 280 tsoc->tsoc_real_dev = dmp_real; 281 282 dm_table_add_deps(table_en, dmp_real); 283 284 dm_table_init_target(table_en, tsoc); 285 286 return 0; 287 } 288 289 /* 290 * Table routine is called to get params string, which is target 291 * specific. When dm_table_status_ioctl is called with flag 292 * DM_STATUS_TABLE_FLAG I have to sent params string back. 293 */ 294 char * 295 dm_target_snapshot_orig_table(void *target_config) 296 { 297 dm_target_snapshot_origin_config_t *tsoc; 298 299 size_t prm_len; 300 char *params; 301 302 tsoc = target_config; 303 304 prm_len = 0; 305 306 kprintf("Snapshot origin target table function called\n"); 307 308 /* length of names + count of chars + spaces and null char */ 309 prm_len = strlen(tsoc->tsoc_real_dev->name) + 1; 310 311 kprintf("real_dev name %s\n", tsoc->tsoc_real_dev->name); 312 313 params = kmem_alloc(prm_len, VM_SUBSYS_DM, KM_NOSLEEP); 314 if (params == NULL) 315 return NULL; 316 317 kprintf("%s\n", tsoc->tsoc_real_dev->name); 318 319 snprintf(params, prm_len, "%s", tsoc->tsoc_real_dev->name); 320 321 return params; 322 } 323 324 /* Strategy routine called from dm_strategy. */ 325 int 326 dm_target_snapshot_orig_strategy(dm_table_entry_t *table_en, struct buf *bp) 327 { 328 329 kprintf("Snapshot_Orig target read function called!!\n"); 330 331 bp->b_error = EIO; 332 bp->b_resid = 0; 333 334 biodone(bp); 335 336 return 0; 337 } 338 339 /* Decrement pdev and free allocated space. */ 340 int 341 dm_target_snapshot_orig_destroy(dm_table_entry_t *table_en) 342 { 343 dm_target_snapshot_origin_config_t *tsoc; 344 345 /* 346 * Destroy function is called for every target even if it 347 * doesn't have target_config. 348 */ 349 350 if (table_en->target_config == NULL) 351 return 0; 352 353 tsoc = table_en->target_config; 354 355 /* Decrement pdev ref counter if 0 remove it */ 356 dm_pdev_decr(tsoc->tsoc_real_dev); 357 358 /* Unbusy target so we can unload it */ 359 dm_target_unbusy(table_en->target); 360 361 kmem_free(table_en->target_config, sizeof(dm_target_snapshot_origin_config_t)); 362 363 return 0; 364 } 365