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/kmem.h>
80 
81 #include <dev/disk/dm/dm.h>
82 
83 typedef struct target_snapshot_config {
84 	dm_pdev_t *tsc_snap_dev;
85 	/* cow dev is set only for persistent snapshot devices */
86 	dm_pdev_t *tsc_cow_dev;
87 
88 	uint64_t tsc_chunk_size;
89 	uint32_t tsc_persistent_dev;
90 } dm_target_snapshot_config_t;
91 
92 typedef struct target_snapshot_origin_config {
93 	dm_pdev_t *tsoc_real_dev;
94 	/* list of snapshots ? */
95 } dm_target_snapshot_origin_config_t;
96 
97 int dm_target_snapshot_init(dm_table_entry_t *, int, char **);
98 char *dm_target_snapshot_table(void *);
99 int dm_target_snapshot_strategy(dm_table_entry_t *, struct buf *);
100 int dm_target_snapshot_destroy(dm_table_entry_t *);
101 
102 int dm_target_snapshot_orig_init(dm_table_entry_t *, int, char **);
103 char *dm_target_snapshot_orig_table(void *);
104 int dm_target_snapshot_orig_strategy(dm_table_entry_t *, struct buf *);
105 int dm_target_snapshot_orig_destroy(dm_table_entry_t *);
106 
107 /*
108  * Init function called from dm_table_load_ioctl.
109  * argv:  /dev/mapper/my_data_org /dev/mapper/tsc_cow_dev p 64
110  *        snapshot_origin device, cow device, persistent flag, chunk size
111  */
112 int
113 dm_target_snapshot_init(dm_table_entry_t *table_en, int argc, char **argv)
114 {
115 	dm_target_snapshot_config_t *tsc;
116 	dm_pdev_t *dmp_snap, *dmp_cow;
117 
118 	dmp_cow = NULL;
119 
120 	kprintf("Snapshot target init function called!!\n");
121 	kprintf("Snapshotted device: %s, cow device %s,\n\t persistent flag: %s, "
122 	    "chunk size: %s\n", argv[0], argv[1], argv[2], argv[3]);
123 
124 	/* Insert snap device to global pdev list */
125 	if ((dmp_snap = dm_pdev_insert(argv[0])) == NULL)
126 		return ENOENT;
127 
128 	if ((tsc = kmem_alloc(sizeof(dm_target_snapshot_config_t), KM_NOSLEEP))
129 	    == NULL)
130 		return 1;
131 
132 	tsc->tsc_persistent_dev = 0;
133 
134 	/* There is now cow device for nonpersistent snapshot devices */
135 	if (strcmp(argv[2], "p") == 0) {
136 		tsc->tsc_persistent_dev = 1;
137 
138 		/* Insert cow device to global pdev list */
139 		if ((dmp_cow = dm_pdev_insert(argv[1])) == NULL)
140 			return ENOENT;
141 	}
142 	tsc->tsc_chunk_size = atoi64(argv[3]);
143 
144 	tsc->tsc_snap_dev = dmp_snap;
145 	tsc->tsc_cow_dev = dmp_cow;
146 
147 	dm_table_add_deps(table_en, dmp_snap);
148 	dm_table_add_deps(table_en, dmp_cow);
149 
150 	dm_table_init_target(table_en, tsc);
151 
152 	return 0;
153 }
154 
155 /*
156  * Table routine is called to get params string, which is target
157  * specific. When dm_table_status_ioctl is called with flag
158  * DM_STATUS_TABLE_FLAG I have to sent params string back.
159  */
160 char *
161 dm_target_snapshot_table(void *target_config)
162 {
163 	dm_target_snapshot_config_t *tsc;
164 
165 	uint32_t i;
166 	uint32_t count;
167 	size_t prm_len, cow_len;
168 	char *params, *cow_name;
169 
170 	tsc = target_config;
171 
172 	prm_len = 0;
173 	cow_len = 0;
174 	count = 0;
175 	cow_name = NULL;
176 
177 	kprintf("Snapshot target table function called\n");
178 
179 	/* count number of chars in offset */
180 	for (i = tsc->tsc_chunk_size; i != 0; i /= 10)
181 		count++;
182 
183 	if (tsc->tsc_persistent_dev)
184 		cow_len = strlen(tsc->tsc_cow_dev->name);
185 
186 	/* length of names + count of chars + spaces and null char */
187 	prm_len = strlen(tsc->tsc_snap_dev->name) + cow_len + count + 5;
188 
189 	params = dm_alloc_string(prm_len);
190 
191 	kprintf("%s %s %s %" PRIu64 "\n", tsc->tsc_snap_dev->name,
192 	    tsc->tsc_cow_dev->name, tsc->tsc_persistent_dev ? "p" : "n",
193 	    tsc->tsc_chunk_size);
194 
195 	ksnprintf(params, prm_len, "%s %s %s %" PRIu64, tsc->tsc_snap_dev->name,
196 	    tsc->tsc_persistent_dev ? tsc->tsc_cow_dev->name : "",
197 	    tsc->tsc_persistent_dev ? "p" : "n",
198 	    tsc->tsc_chunk_size);
199 
200 	return params;
201 }
202 
203 /* Strategy routine called from dm_strategy. */
204 int
205 dm_target_snapshot_strategy(dm_table_entry_t *table_en, struct buf *bp)
206 {
207 
208 	kprintf("Snapshot target read function called!!\n");
209 
210 	bp->b_error = EIO;
211 	bp->b_resid = 0;
212 
213 	biodone(bp);
214 
215 	return 0;
216 }
217 
218 /* Doesn't do anything here. */
219 int
220 dm_target_snapshot_destroy(dm_table_entry_t *table_en)
221 {
222 	dm_target_snapshot_config_t *tsc;
223 
224 	/*
225 	 * Destroy function is called for every target even if it
226 	 * doesn't have target_config.
227 	 */
228 
229 	if (table_en->target_config == NULL)
230 		return 0;
231 
232 	kprintf("Snapshot target destroy function called\n");
233 
234 	tsc = table_en->target_config;
235 
236 	/* Decrement pdev ref counter if 0 remove it */
237 	dm_pdev_decr(tsc->tsc_snap_dev);
238 
239 	if (tsc->tsc_persistent_dev)
240 		dm_pdev_decr(tsc->tsc_cow_dev);
241 
242 	kmem_free(table_en->target_config, sizeof(dm_target_snapshot_config_t));
243 
244 	return 0;
245 }
246 
247 /*
248  * dm target snapshot origin routines.
249  *
250  * Keep for compatibility with linux lvm2tools. They use two targets
251  * to implement snapshots. Snapshot target will implement exception
252  * store and snapshot origin will implement device which calls every
253  * snapshot device when write is done on master device.
254  */
255 
256 /*
257  * Init function called from dm_table_load_ioctl.
258  *
259  * argv: /dev/mapper/my_data_real
260  */
261 int
262 dm_target_snapshot_orig_init(dm_table_entry_t *table_en, int argc, char **argv)
263 {
264 	dm_target_snapshot_origin_config_t *tsoc;
265 	dm_pdev_t *dmp_real;
266 
267 	kprintf("Snapshot origin target init function called!!\n");
268 	kprintf("Parent device: %s\n", argv[0]);
269 
270 	/* Insert snap device to global pdev list */
271 	if ((dmp_real = dm_pdev_insert(argv[0])) == NULL)
272 		return ENOENT;
273 
274 	if ((tsoc = kmem_alloc(sizeof(dm_target_snapshot_origin_config_t), KM_NOSLEEP))
275 	    == NULL)
276 		return 1;
277 
278 	tsoc->tsoc_real_dev = dmp_real;
279 
280 	dm_table_add_deps(table_en, dmp_real);
281 
282 	dm_table_init_target(table_en, tsoc);
283 
284 	return 0;
285 }
286 
287 /*
288  * Table routine is called to get params string, which is target
289  * specific. When dm_table_status_ioctl is called with flag
290  * DM_STATUS_TABLE_FLAG I have to sent params string back.
291  */
292 char *
293 dm_target_snapshot_orig_table(void *target_config)
294 {
295 	dm_target_snapshot_origin_config_t *tsoc;
296 
297 	size_t prm_len;
298 	char *params;
299 
300 	tsoc = target_config;
301 
302 	prm_len = 0;
303 
304 	kprintf("Snapshot origin target table function called\n");
305 
306 	/* length of names + count of chars + spaces and null char */
307 	prm_len = strlen(tsoc->tsoc_real_dev->name) + 1;
308 
309 	kprintf("real_dev name %s\n", tsoc->tsoc_real_dev->name);
310 
311 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
312 		return NULL;
313 
314 	kprintf("%s\n", tsoc->tsoc_real_dev->name);
315 
316 	snprintf(params, prm_len, "%s", tsoc->tsoc_real_dev->name);
317 
318 	return params;
319 }
320 
321 /* Strategy routine called from dm_strategy. */
322 int
323 dm_target_snapshot_orig_strategy(dm_table_entry_t *table_en, struct buf *bp)
324 {
325 
326 	kprintf("Snapshot_Orig target read function called!!\n");
327 
328 	bp->b_error = EIO;
329 	bp->b_resid = 0;
330 
331 	biodone(bp);
332 
333 	return 0;
334 }
335 
336 /* Decrement pdev and free allocated space. */
337 int
338 dm_target_snapshot_orig_destroy(dm_table_entry_t *table_en)
339 {
340 	dm_target_snapshot_origin_config_t *tsoc;
341 
342 	/*
343 	 * Destroy function is called for every target even if it
344 	 * doesn't have target_config.
345 	 */
346 
347 	if (table_en->target_config == NULL)
348 		return 0;
349 
350 	tsoc = table_en->target_config;
351 
352 	/* Decrement pdev ref counter if 0 remove it */
353 	dm_pdev_decr(tsoc->tsoc_real_dev);
354 
355 	/* Unbusy target so we can unload it */
356 	dm_target_unbusy(table_en->target);
357 
358 	kmem_free(table_en->target_config, sizeof(dm_target_snapshot_origin_config_t));
359 
360 	return 0;
361 }
362