dm_target_snapshot.c revision 1.3 1 1.3 haad /* $NetBSD: dm_target_snapshot.c,v 1.3 2008/12/19 16:30:41 haad Exp $ */
2 1.2 haad
3 1.2 haad /*
4 1.2 haad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.2 haad * All rights reserved.
6 1.2 haad *
7 1.2 haad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 haad * by Adam Hamsik.
9 1.2 haad *
10 1.2 haad * Redistribution and use in source and binary forms, with or without
11 1.2 haad * modification, are permitted provided that the following conditions
12 1.2 haad * are met:
13 1.2 haad * 1. Redistributions of source code must retain the above copyright
14 1.2 haad * notice, this list of conditions and the following disclaimer.
15 1.2 haad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 haad * notice, this list of conditions and the following disclaimer in the
17 1.2 haad * documentation and/or other materials provided with the distribution.
18 1.2 haad *
19 1.2 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 haad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 haad */
31 1.2 haad
32 1.2 haad /*
33 1.2 haad * 1. Suspend my_data to temporarily stop any I/O while the snapshot is being
34 1.2 haad * activated.
35 1.2 haad * dmsetup suspend my_data
36 1.2 haad *
37 1.2 haad * 2. Create the snapshot-origin device with no table.
38 1.2 haad * dmsetup create my_data_org
39 1.2 haad *
40 1.2 haad * 3. Read the table from my_data and load it into my_data_org.
41 1.2 haad * dmsetup table my_data | dmsetup load my_data_org
42 1.2 haad *
43 1.2 haad * 4. Resume this new table.
44 1.2 haad * dmsetup resume my_data_org
45 1.2 haad *
46 1.2 haad * 5. Create the snapshot device with no table.
47 1.2 haad * dmsetup create my_data_snap
48 1.2 haad *
49 1.2 haad * 6. Load the table into my_data_snap. This uses /dev/hdd1 as the COW device and
50 1.2 haad * uses a 32kB chunk-size.
51 1.2 haad * echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot \
52 1.2 haad * /dev/mapper/my_data_org /dev/hdd1 p 64" | dmsetup load my_data_snap
53 1.2 haad *
54 1.2 haad * 7. Reload my_data as a snapshot-origin device that points to my_data_org.
55 1.2 haad * echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot-origin \
56 1.2 haad * /dev/mapper/my_data_org" | dmsetup load my_data
57 1.2 haad *
58 1.2 haad * 8. Resume the snapshot and origin devices.
59 1.2 haad * dmsetup resume my_data_snap
60 1.2 haad * dmsetup resume my_data
61 1.2 haad */
62 1.2 haad
63 1.2 haad /*
64 1.2 haad * This file implements initial version of device-mapper snapshot target.
65 1.2 haad */
66 1.2 haad #include <sys/types.h>
67 1.2 haad #include <sys/param.h>
68 1.2 haad
69 1.2 haad #include <sys/buf.h>
70 1.2 haad #include <sys/kmem.h>
71 1.2 haad #include <sys/vnode.h>
72 1.2 haad
73 1.2 haad #include "dm.h"
74 1.2 haad
75 1.2 haad /*
76 1.2 haad * Init function called from dm_table_load_ioctl.
77 1.2 haad * argv: /dev/mapper/my_data_org /dev/mapper/tsc_cow_dev p 64
78 1.2 haad * snapshot_origin device, cow device, persistent flag, chunk size
79 1.2 haad */
80 1.2 haad int
81 1.2 haad dm_target_snapshot_init(dm_dev_t *dmv, void **target_config, char *params)
82 1.2 haad {
83 1.2 haad dm_target_snapshot_config_t *tsc;
84 1.2 haad dm_pdev_t *dmp_snap, *dmp_cow;
85 1.2 haad char **ap, *argv[5];
86 1.2 haad
87 1.2 haad dmp_cow = NULL;
88 1.2 haad
89 1.2 haad if (params == NULL)
90 1.2 haad return EINVAL;
91 1.2 haad /*
92 1.2 haad * Parse a string, containing tokens delimited by white space,
93 1.2 haad * into an argument vector
94 1.2 haad */
95 1.2 haad for (ap = argv; ap < &argv[4] &&
96 1.2 haad (*ap = strsep(¶ms, " \t")) != NULL;) {
97 1.2 haad if (**ap != '\0')
98 1.2 haad ap++;
99 1.2 haad }
100 1.2 haad
101 1.2 haad printf("Snapshot target init function called!!\n");
102 1.2 haad printf("Snapshotted device: %s, cow device %s,\n\t persistent flag: %s, "
103 1.2 haad "chunk size: %s\n", argv[0], argv[1], argv[2], argv[3]);
104 1.2 haad
105 1.2 haad /* Insert snap device to global pdev list */
106 1.2 haad if ((dmp_snap = dm_pdev_insert(argv[0])) == NULL)
107 1.2 haad return ENOENT;
108 1.2 haad
109 1.2 haad if ((tsc = kmem_alloc(sizeof(dm_target_snapshot_config_t), KM_NOSLEEP))
110 1.2 haad == NULL)
111 1.2 haad return 1;
112 1.2 haad
113 1.2 haad tsc->tsc_persistent_dev = 0;
114 1.2 haad
115 1.2 haad /* There is now cow device for nonpersistent snapshot devices */
116 1.2 haad if (strcmp(argv[2], "p") == 0) {
117 1.2 haad tsc->tsc_persistent_dev = 1;
118 1.2 haad
119 1.2 haad /* Insert cow device to global pdev list */
120 1.2 haad if ((dmp_cow = dm_pdev_insert(argv[1])) == NULL)
121 1.2 haad return ENOENT;
122 1.2 haad }
123 1.2 haad
124 1.2 haad tsc->tsc_chunk_size = atoi(argv[3]);
125 1.2 haad
126 1.2 haad tsc->tsc_snap_dev = dmp_snap;
127 1.2 haad tsc->tsc_cow_dev = dmp_cow;
128 1.2 haad
129 1.2 haad *target_config = tsc;
130 1.2 haad
131 1.2 haad dmv->dev_type = DM_SNAPSHOT_DEV;
132 1.2 haad
133 1.2 haad return 0;
134 1.2 haad }
135 1.2 haad
136 1.2 haad /*
137 1.2 haad * Status routine is called to get params string, which is target
138 1.2 haad * specific. When dm_table_status_ioctl is called with flag
139 1.2 haad * DM_STATUS_TABLE_FLAG I have to sent params string back.
140 1.2 haad */
141 1.2 haad char *
142 1.2 haad dm_target_snapshot_status(void *target_config)
143 1.2 haad {
144 1.2 haad dm_target_snapshot_config_t *tsc;
145 1.2 haad
146 1.2 haad uint32_t i;
147 1.2 haad uint32_t count;
148 1.2 haad size_t prm_len, cow_len;
149 1.2 haad char *params, *cow_name;
150 1.2 haad
151 1.2 haad tsc = target_config;
152 1.2 haad
153 1.2 haad prm_len = 0;
154 1.2 haad cow_len = 0;
155 1.2 haad count = 0;
156 1.2 haad cow_name = NULL;
157 1.2 haad
158 1.2 haad printf("Snapshot target status function called\n");
159 1.2 haad
160 1.2 haad /* count number of chars in offset */
161 1.2 haad for(i = tsc->tsc_chunk_size; i != 0; i /= 10)
162 1.2 haad count++;
163 1.2 haad
164 1.2 haad if(tsc->tsc_persistent_dev)
165 1.2 haad cow_len = strlen(tsc->tsc_cow_dev->name);
166 1.2 haad
167 1.2 haad /* length of names + count of chars + spaces and null char */
168 1.2 haad prm_len = strlen(tsc->tsc_snap_dev->name) + cow_len + count + 5;
169 1.2 haad
170 1.2 haad if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
171 1.2 haad return NULL;
172 1.2 haad
173 1.2 haad printf("%s %s %s %"PRIu64"\n", tsc->tsc_snap_dev->name,
174 1.2 haad tsc->tsc_cow_dev->name, tsc->tsc_persistent_dev ? "p":"n",
175 1.2 haad tsc->tsc_chunk_size);
176 1.2 haad
177 1.2 haad snprintf(params, prm_len, "%s %s %s %"PRIu64, tsc->tsc_snap_dev->name,
178 1.2 haad tsc->tsc_persistent_dev ? tsc->tsc_cow_dev->name : "",
179 1.2 haad tsc->tsc_persistent_dev ? "p":"n",
180 1.2 haad tsc->tsc_chunk_size);
181 1.2 haad
182 1.2 haad return params;
183 1.2 haad }
184 1.2 haad
185 1.2 haad /* Strategy routine called from dm_strategy. */
186 1.2 haad int
187 1.2 haad dm_target_snapshot_strategy(dm_table_entry_t *table_en, struct buf *bp)
188 1.2 haad {
189 1.2 haad
190 1.2 haad printf("Snapshot target read function called!!\n");
191 1.2 haad
192 1.2 haad bp->b_error = EIO;
193 1.2 haad bp->b_resid = 0;
194 1.2 haad
195 1.2 haad biodone(bp);
196 1.2 haad
197 1.2 haad return 0;
198 1.2 haad }
199 1.2 haad
200 1.2 haad /* Doesn't do anything here. */
201 1.2 haad int
202 1.2 haad dm_target_snapshot_destroy(dm_table_entry_t *table_en)
203 1.2 haad {
204 1.2 haad dm_target_snapshot_config_t *tsc;
205 1.2 haad
206 1.2 haad /*
207 1.2 haad * Destroy function is called for every target even if it
208 1.2 haad * doesn't have target_config.
209 1.2 haad */
210 1.2 haad
211 1.2 haad if (table_en->target_config == NULL)
212 1.2 haad return 0;
213 1.2 haad
214 1.2 haad printf("Snapshot target destroy function called\n");
215 1.2 haad
216 1.2 haad tsc = table_en->target_config;
217 1.2 haad
218 1.2 haad /* Decrement pdev ref counter if 0 remove it */
219 1.2 haad dm_pdev_decr(tsc->tsc_snap_dev);
220 1.2 haad
221 1.2 haad if (tsc->tsc_persistent_dev)
222 1.2 haad dm_pdev_decr(tsc->tsc_cow_dev);
223 1.3 haad
224 1.3 haad /* Unbusy target so we can unload it */
225 1.3 haad dm_target_unbusy(table_en->target);
226 1.2 haad
227 1.2 haad kmem_free(table_en->target_config, sizeof(dm_target_snapshot_config_t));
228 1.2 haad
229 1.2 haad table_en->target_config = NULL;
230 1.2 haad
231 1.2 haad return 0;
232 1.2 haad }
233 1.2 haad
234 1.2 haad /* Add this target dependiences to prop_array_t */
235 1.2 haad int
236 1.2 haad dm_target_snapshot_deps(dm_table_entry_t *table_en,
237 1.2 haad prop_array_t prop_array)
238 1.2 haad {
239 1.2 haad dm_target_snapshot_config_t *tsc;
240 1.2 haad struct vattr va;
241 1.2 haad
242 1.2 haad int error;
243 1.2 haad
244 1.2 haad if (table_en->target_config == NULL)
245 1.2 haad return 0;
246 1.2 haad
247 1.2 haad tsc = table_en->target_config;
248 1.2 haad
249 1.2 haad if ((error = VOP_GETATTR(tsc->tsc_snap_dev->pdev_vnode, &va, curlwp->l_cred)) != 0)
250 1.2 haad return error;
251 1.2 haad
252 1.2 haad prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
253 1.2 haad
254 1.2 haad if (tsc->tsc_persistent_dev) {
255 1.2 haad
256 1.2 haad if ((error = VOP_GETATTR(tsc->tsc_cow_dev->pdev_vnode, &va,
257 1.2 haad curlwp->l_cred)) != 0)
258 1.2 haad return error;
259 1.2 haad
260 1.2 haad prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
261 1.2 haad
262 1.2 haad }
263 1.2 haad
264 1.2 haad return 0;
265 1.2 haad }
266 1.2 haad
267 1.2 haad /* Upcall is used to inform other depended devices about IO. */
268 1.2 haad int
269 1.2 haad dm_target_snapshot_upcall(dm_table_entry_t *table_en, struct buf *bp)
270 1.2 haad {
271 1.2 haad printf("dm_target_snapshot_upcall called\n");
272 1.2 haad
273 1.2 haad printf("upcall buf flags %s %s\n",
274 1.2 haad (bp->b_flags & B_WRITE) ? "B_WRITE":"",
275 1.2 haad (bp->b_flags & B_READ) ? "B_READ":"");
276 1.2 haad
277 1.2 haad return 0;
278 1.2 haad }
279 1.2 haad
280 1.2 haad /*
281 1.2 haad * dm target snapshot origin routines.
282 1.2 haad *
283 1.2 haad * Keep for compatibility with linux lvm2tools. They use two targets
284 1.2 haad * to implement snapshots. Snapshot target will implement exception
285 1.2 haad * store and snapshot origin will implement device which calls every
286 1.2 haad * snapshot device when write is done on master device.
287 1.2 haad */
288 1.2 haad
289 1.2 haad /*
290 1.2 haad * Init function called from dm_table_load_ioctl.
291 1.2 haad *
292 1.2 haad * argv: /dev/mapper/my_data_real
293 1.2 haad */
294 1.2 haad int
295 1.2 haad dm_target_snapshot_orig_init(dm_dev_t *dmv, void **target_config,
296 1.2 haad char *params)
297 1.2 haad {
298 1.2 haad dm_target_snapshot_origin_config_t *tsoc;
299 1.2 haad dm_pdev_t *dmp_real;
300 1.2 haad
301 1.2 haad if (params == NULL)
302 1.2 haad return EINVAL;
303 1.2 haad
304 1.2 haad printf("Snapshot origin target init function called!!\n");
305 1.2 haad printf("Parent device: %s\n", params);
306 1.2 haad
307 1.2 haad /* Insert snap device to global pdev list */
308 1.2 haad if ((dmp_real = dm_pdev_insert(params)) == NULL)
309 1.2 haad return ENOENT;
310 1.2 haad
311 1.2 haad if ((tsoc = kmem_alloc(sizeof(dm_target_snapshot_origin_config_t), KM_NOSLEEP))
312 1.2 haad == NULL)
313 1.2 haad return 1;
314 1.2 haad
315 1.2 haad tsoc->tsoc_real_dev = dmp_real;
316 1.2 haad
317 1.2 haad dmv->dev_type = DM_SNAPSHOT_ORIG_DEV;
318 1.2 haad
319 1.2 haad *target_config = tsoc;
320 1.2 haad
321 1.2 haad return 0;
322 1.2 haad }
323 1.2 haad
324 1.2 haad /*
325 1.2 haad * Status routine is called to get params string, which is target
326 1.2 haad * specific. When dm_table_status_ioctl is called with flag
327 1.2 haad * DM_STATUS_TABLE_FLAG I have to sent params string back.
328 1.2 haad */
329 1.2 haad char *
330 1.2 haad dm_target_snapshot_orig_status(void *target_config)
331 1.2 haad {
332 1.2 haad dm_target_snapshot_origin_config_t *tsoc;
333 1.2 haad
334 1.2 haad size_t prm_len;
335 1.2 haad char *params;
336 1.2 haad
337 1.2 haad tsoc = target_config;
338 1.2 haad
339 1.2 haad prm_len = 0;
340 1.2 haad
341 1.2 haad printf("Snapshot origin target status function called\n");
342 1.2 haad
343 1.2 haad /* length of names + count of chars + spaces and null char */
344 1.2 haad prm_len = strlen(tsoc->tsoc_real_dev->name) + 1;
345 1.2 haad
346 1.2 haad printf("real_dev name %s\n",tsoc->tsoc_real_dev->name);
347 1.2 haad
348 1.2 haad if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
349 1.2 haad return NULL;
350 1.2 haad
351 1.2 haad printf("%s\n", tsoc->tsoc_real_dev->name);
352 1.2 haad
353 1.2 haad snprintf(params, prm_len, "%s", tsoc->tsoc_real_dev->name);
354 1.2 haad
355 1.2 haad return params;
356 1.2 haad }
357 1.2 haad
358 1.2 haad /* Strategy routine called from dm_strategy. */
359 1.2 haad int
360 1.2 haad dm_target_snapshot_orig_strategy(dm_table_entry_t *table_en, struct buf *bp)
361 1.2 haad {
362 1.2 haad
363 1.2 haad printf("Snapshot_Orig target read function called!!\n");
364 1.2 haad
365 1.2 haad bp->b_error = EIO;
366 1.2 haad bp->b_resid = 0;
367 1.2 haad
368 1.2 haad biodone(bp);
369 1.2 haad
370 1.2 haad return 0;
371 1.2 haad }
372 1.2 haad
373 1.2 haad /* Decrement pdev and free allocated space. */
374 1.2 haad int
375 1.2 haad dm_target_snapshot_orig_destroy(dm_table_entry_t *table_en)
376 1.2 haad {
377 1.2 haad dm_target_snapshot_origin_config_t *tsoc;
378 1.2 haad
379 1.2 haad /*
380 1.2 haad * Destroy function is called for every target even if it
381 1.2 haad * doesn't have target_config.
382 1.2 haad */
383 1.2 haad
384 1.2 haad if (table_en->target_config == NULL)
385 1.2 haad return 0;
386 1.2 haad
387 1.2 haad tsoc = table_en->target_config;
388 1.2 haad
389 1.2 haad /* Decrement pdev ref counter if 0 remove it */
390 1.2 haad dm_pdev_decr(tsoc->tsoc_real_dev);
391 1.3 haad
392 1.3 haad /* Unbusy target so we can unload it */
393 1.3 haad dm_target_unbusy(table_en->target);
394 1.2 haad
395 1.2 haad kmem_free(table_en->target_config, sizeof(dm_target_snapshot_origin_config_t));
396 1.2 haad
397 1.2 haad table_en->target_config = NULL;
398 1.2 haad
399 1.2 haad return 0;
400 1.2 haad }
401 1.2 haad
402 1.2 haad /*
403 1.2 haad * Get target deps and add them to prop_array_t.
404 1.2 haad */
405 1.2 haad int
406 1.2 haad dm_target_snapshot_orig_deps(dm_table_entry_t *table_en,
407 1.2 haad prop_array_t prop_array)
408 1.2 haad {
409 1.2 haad dm_target_snapshot_origin_config_t *tsoc;
410 1.2 haad struct vattr va;
411 1.2 haad
412 1.2 haad int error;
413 1.2 haad
414 1.2 haad if (table_en->target_config == NULL)
415 1.2 haad return 0;
416 1.2 haad
417 1.2 haad tsoc = table_en->target_config;
418 1.2 haad
419 1.2 haad if ((error = VOP_GETATTR(tsoc->tsoc_real_dev->pdev_vnode, &va,
420 1.2 haad curlwp->l_cred)) != 0)
421 1.2 haad return error;
422 1.2 haad
423 1.2 haad prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
424 1.2 haad
425 1.2 haad return 0;
426 1.2 haad }
427 1.2 haad
428 1.2 haad /* Unsupported for this target. */
429 1.2 haad int
430 1.2 haad dm_target_snapshot_orig_upcall(dm_table_entry_t *table_en, struct buf *bp)
431 1.2 haad {
432 1.2 haad return 0;
433 1.2 haad }
434