xref: /netbsd-src/sys/dev/dm/dm_target_snapshot.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*        $NetBSD: dm_target_snapshot.c,v 1.6 2009/01/02 11:06:17 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, NULL);
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 ((r = module_hold("dm")) != 0)
111 			return r;
112 
113 		if (((dmt = dm_target_lookup("snapshot")) != NULL) ||
114 		    (((dmt = dm_target_lookup("snapshot-origin")) != NULL)))
115 			return EEXIST;
116 
117 		dmt = dm_target_alloc("snapshot");
118 		dmt1 = dm_target_alloc("snapshot-origin");
119 
120 		dmt->version[0] = 1;
121 		dmt->version[1] = 0;
122 		dmt->version[2] = 5;
123 		strlcpy(dmt->name, "snapshot", DM_MAX_TYPE_NAME);
124 		dmt->init = &dm_target_snapshot_init;
125 		dmt->status = &dm_target_snapshot_status;
126 		dmt->strategy = &dm_target_snapshot_strategy;
127 		dmt->deps = &dm_target_snapshot_deps;
128 		dmt->destroy = &dm_target_snapshot_destroy;
129 		dmt->upcall = &dm_target_snapshot_upcall;
130 
131 		r = dm_target_insert(dmt);
132 
133 		dmt1->version[0] = 1;
134 		dmt1->version[1] = 0;
135 		dmt1->version[2] = 5;
136 		strlcpy(dmt1->name, "snapshot-origin", DM_MAX_TYPE_NAME);
137 		dmt1->init = &dm_target_snapshot_orig_init;
138 		dmt1->status = &dm_target_snapshot_orig_status;
139 		dmt1->strategy = &dm_target_snapshot_orig_strategy;
140 		dmt1->deps = &dm_target_snapshot_orig_deps;
141 		dmt1->destroy = &dm_target_snapshot_orig_destroy;
142 		dmt1->upcall = &dm_target_snapshot_orig_upcall;
143 
144 		r = dm_target_insert(dmt1);
145 		break;
146 
147 	case MODULE_CMD_FINI:
148 		/*
149 		 * Try to remove snapshot target if it works remove snap-origin
150 		 * it is not possible to remove snapshot and do not remove
151 		 * snap-origin because they are used together.
152 		 */
153 		if ((r = dm_target_rem("snapshot")) == 0)
154 			r = dm_target_rem("snapshot-origin");
155 
156 		module_rele("dm"); /* release usage counter on dm module */
157 
158 		break;
159 
160 	case MODULE_CMD_STAT:
161 		return ENOTTY;
162 
163 	default:
164 		return ENOTTY;
165 	}
166 
167 	return r;
168 }
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 
221 	tsc->tsc_chunk_size = atoi(argv[3]);
222 
223 	tsc->tsc_snap_dev = dmp_snap;
224 	tsc->tsc_cow_dev = dmp_cow;
225 
226 	*target_config = tsc;
227 
228 	dmv->dev_type = DM_SNAPSHOT_DEV;
229 
230 	return 0;
231 }
232 
233 /*
234  * Status routine is called to get params string, which is target
235  * specific. When dm_table_status_ioctl is called with flag
236  * DM_STATUS_TABLE_FLAG I have to sent params string back.
237  */
238 char *
239 dm_target_snapshot_status(void *target_config)
240 {
241 	dm_target_snapshot_config_t *tsc;
242 
243 	uint32_t i;
244 	uint32_t count;
245 	size_t prm_len, cow_len;
246 	char *params, *cow_name;
247 
248 	tsc = target_config;
249 
250 	prm_len = 0;
251 	cow_len = 0;
252 	count = 0;
253 	cow_name = NULL;
254 
255 	printf("Snapshot target status function called\n");
256 
257 	/* count number of chars in offset */
258 	for(i = tsc->tsc_chunk_size; i != 0; i /= 10)
259 		count++;
260 
261 	if(tsc->tsc_persistent_dev)
262 		cow_len = strlen(tsc->tsc_cow_dev->name);
263 
264 	/* length of names + count of chars + spaces and null char */
265 	prm_len = strlen(tsc->tsc_snap_dev->name) + cow_len + count + 5;
266 
267 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
268 		return NULL;
269 
270 	printf("%s %s %s %"PRIu64"\n", tsc->tsc_snap_dev->name,
271 		tsc->tsc_cow_dev->name, tsc->tsc_persistent_dev ? "p":"n",
272 		tsc->tsc_chunk_size);
273 
274 	snprintf(params, prm_len, "%s %s %s %"PRIu64, tsc->tsc_snap_dev->name,
275 		tsc->tsc_persistent_dev ? tsc->tsc_cow_dev->name : "",
276 		tsc->tsc_persistent_dev ? "p":"n",
277 		tsc->tsc_chunk_size);
278 
279 	return params;
280 }
281 
282 /* Strategy routine called from dm_strategy. */
283 int
284 dm_target_snapshot_strategy(dm_table_entry_t *table_en, struct buf *bp)
285 {
286 
287 	printf("Snapshot target read function called!!\n");
288 
289 	bp->b_error = EIO;
290 	bp->b_resid = 0;
291 
292 	biodone(bp);
293 
294 	return 0;
295 }
296 
297 /* Doesn't do anything here. */
298 int
299 dm_target_snapshot_destroy(dm_table_entry_t *table_en)
300 {
301 	dm_target_snapshot_config_t *tsc;
302 
303 	/*
304 	 * Destroy function is called for every target even if it
305 	 * doesn't have target_config.
306 	 */
307 
308 	if (table_en->target_config == NULL)
309 		return 0;
310 
311 	printf("Snapshot target destroy function called\n");
312 
313 	tsc = table_en->target_config;
314 
315 	/* Decrement pdev ref counter if 0 remove it */
316 	dm_pdev_decr(tsc->tsc_snap_dev);
317 
318 	if (tsc->tsc_persistent_dev)
319 		dm_pdev_decr(tsc->tsc_cow_dev);
320 
321         /* Unbusy target so we can unload it */
322 	dm_target_unbusy(table_en->target);
323 
324 	kmem_free(table_en->target_config, sizeof(dm_target_snapshot_config_t));
325 
326 	table_en->target_config = NULL;
327 
328 	return 0;
329 }
330 
331 /* Add this target dependiences to prop_array_t */
332 int
333 dm_target_snapshot_deps(dm_table_entry_t *table_en,
334 	prop_array_t prop_array)
335 {
336 	dm_target_snapshot_config_t *tsc;
337 	struct vattr va;
338 
339 	int error;
340 
341 	if (table_en->target_config == NULL)
342 		return 0;
343 
344 	tsc = table_en->target_config;
345 
346 	if ((error = VOP_GETATTR(tsc->tsc_snap_dev->pdev_vnode, &va, curlwp->l_cred)) != 0)
347 		return error;
348 
349 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
350 
351 	if (tsc->tsc_persistent_dev) {
352 
353 		if ((error = VOP_GETATTR(tsc->tsc_cow_dev->pdev_vnode, &va,
354 				curlwp->l_cred)) != 0)
355 			return error;
356 
357 		prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
358 
359 	}
360 
361 	return 0;
362 }
363 
364 /* Upcall is used to inform other depended devices about IO. */
365 int
366 dm_target_snapshot_upcall(dm_table_entry_t *table_en, struct buf *bp)
367 {
368 	printf("dm_target_snapshot_upcall called\n");
369 
370 	printf("upcall buf flags %s %s\n",
371 		(bp->b_flags & B_WRITE) ? "B_WRITE":"",
372 		(bp->b_flags & B_READ) ? "B_READ":"");
373 
374 	return 0;
375 }
376 
377 /*
378  * dm target snapshot origin routines.
379  *
380  * Keep for compatibility with linux lvm2tools. They use two targets
381  * to implement snapshots. Snapshot target will implement exception
382  * store and snapshot origin will implement device which calls every
383  * snapshot device when write is done on master device.
384  */
385 
386 /*
387  * Init function called from dm_table_load_ioctl.
388  *
389  * argv: /dev/mapper/my_data_real
390  */
391 int
392 dm_target_snapshot_orig_init(dm_dev_t *dmv, void **target_config,
393 	char *params)
394 {
395 	dm_target_snapshot_origin_config_t *tsoc;
396 	dm_pdev_t *dmp_real;
397 
398 	if (params == NULL)
399 		return EINVAL;
400 
401 	printf("Snapshot origin target init function called!!\n");
402 	printf("Parent device: %s\n", params);
403 
404 	/* Insert snap device to global pdev list */
405 	if ((dmp_real = dm_pdev_insert(params)) == NULL)
406 		return ENOENT;
407 
408 	if ((tsoc = kmem_alloc(sizeof(dm_target_snapshot_origin_config_t), KM_NOSLEEP))
409 	    == NULL)
410 		return 1;
411 
412 	tsoc->tsoc_real_dev = dmp_real;
413 
414 	dmv->dev_type = DM_SNAPSHOT_ORIG_DEV;
415 
416 	*target_config = tsoc;
417 
418 	return 0;
419 }
420 
421 /*
422  * Status routine is called to get params string, which is target
423  * specific. When dm_table_status_ioctl is called with flag
424  * DM_STATUS_TABLE_FLAG I have to sent params string back.
425  */
426 char *
427 dm_target_snapshot_orig_status(void *target_config)
428 {
429 	dm_target_snapshot_origin_config_t *tsoc;
430 
431 	size_t prm_len;
432 	char *params;
433 
434 	tsoc = target_config;
435 
436 	prm_len = 0;
437 
438 	printf("Snapshot origin target status function called\n");
439 
440 	/* length of names + count of chars + spaces and null char */
441 	prm_len = strlen(tsoc->tsoc_real_dev->name) + 1;
442 
443 	printf("real_dev name %s\n",tsoc->tsoc_real_dev->name);
444 
445 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
446 		return NULL;
447 
448 	printf("%s\n", tsoc->tsoc_real_dev->name);
449 
450 	snprintf(params, prm_len, "%s", tsoc->tsoc_real_dev->name);
451 
452 	return params;
453 }
454 
455 /* Strategy routine called from dm_strategy. */
456 int
457 dm_target_snapshot_orig_strategy(dm_table_entry_t *table_en, struct buf *bp)
458 {
459 
460 	printf("Snapshot_Orig target read function called!!\n");
461 
462 	bp->b_error = EIO;
463 	bp->b_resid = 0;
464 
465 	biodone(bp);
466 
467 	return 0;
468 }
469 
470 /* Decrement pdev and free allocated space. */
471 int
472 dm_target_snapshot_orig_destroy(dm_table_entry_t *table_en)
473 {
474 	dm_target_snapshot_origin_config_t *tsoc;
475 
476 	/*
477 	 * Destroy function is called for every target even if it
478 	 * doesn't have target_config.
479 	 */
480 
481 	if (table_en->target_config == NULL)
482 		return 0;
483 
484 	tsoc = table_en->target_config;
485 
486 	/* Decrement pdev ref counter if 0 remove it */
487 	dm_pdev_decr(tsoc->tsoc_real_dev);
488 
489 	/* Unbusy target so we can unload it */
490 	dm_target_unbusy(table_en->target);
491 
492 	kmem_free(table_en->target_config, sizeof(dm_target_snapshot_origin_config_t));
493 
494 	table_en->target_config = NULL;
495 
496 	return 0;
497 }
498 
499 /*
500  * Get target deps and add them to prop_array_t.
501  */
502 int
503 dm_target_snapshot_orig_deps(dm_table_entry_t *table_en,
504  	prop_array_t prop_array)
505 {
506 	dm_target_snapshot_origin_config_t *tsoc;
507 	struct vattr va;
508 
509 	int error;
510 
511 	if (table_en->target_config == NULL)
512 		return 0;
513 
514 	tsoc = table_en->target_config;
515 
516 	if ((error = VOP_GETATTR(tsoc->tsoc_real_dev->pdev_vnode, &va,
517 			curlwp->l_cred)) != 0)
518 		return error;
519 
520 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
521 
522 	return 0;
523 }
524 
525 /* Unsupported for this target. */
526 int
527 dm_target_snapshot_orig_upcall(dm_table_entry_t *table_en, struct buf *bp)
528 {
529 	return 0;
530 }
531