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