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