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