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