dm_target_snapshot.c revision 1.1.2.6 1 1.1.2.6 haad /* $NetBSD: dm_target_snapshot.c,v 1.1.2.6 2008/08/20 00:45:47 haad Exp $ */
2 1.1.2.5 haad
3 1.1.2.1 haad /*
4 1.1.2.1 haad * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 1.1.2.1 haad * All rights reserved.
6 1.1.2.1 haad *
7 1.1.2.1 haad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 haad * by Adam Hamsik.
9 1.1.2.1 haad *
10 1.1.2.1 haad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 haad * modification, are permitted provided that the following conditions
12 1.1.2.1 haad * are met:
13 1.1.2.1 haad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 haad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 haad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 haad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 haad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 haad *
19 1.1.2.1 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 haad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 haad */
31 1.1.2.1 haad
32 1.1.2.1 haad /*
33 1.1.2.6 haad * 1. Suspend my_data to temporarily stop any I/O while the snapshot is being
34 1.1.2.6 haad * activated.
35 1.1.2.6 haad * dmsetup suspend my_data
36 1.1.2.6 haad *
37 1.1.2.6 haad * 2. Create the snapshot-origin device with no table.
38 1.1.2.6 haad * dmsetup create my_data_org
39 1.1.2.6 haad *
40 1.1.2.6 haad * 3. Read the table from my_data and load it into my_data_org.
41 1.1.2.6 haad * dmsetup table my_data | dmsetup load my_data_org
42 1.1.2.6 haad *
43 1.1.2.6 haad * 4. Resume this new table.
44 1.1.2.6 haad * dmsetup resume my_data_org
45 1.1.2.6 haad *
46 1.1.2.6 haad * 5. Create the snapshot device with no table.
47 1.1.2.6 haad * dmsetup create my_data_snap
48 1.1.2.6 haad *
49 1.1.2.6 haad * 6. Load the table into my_data_snap. This uses /dev/hdd1 as the COW device and
50 1.1.2.6 haad * uses a 32kB chunk-size.
51 1.1.2.6 haad * echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot \
52 1.1.2.6 haad * /dev/mapper/my_data_org /dev/hdd1 p 64" | dmsetup load my_data_snap
53 1.1.2.6 haad *
54 1.1.2.6 haad * 7. Reload my_data as a snapshot-origin device that points to my_data_org.
55 1.1.2.6 haad * echo "0 `blockdev --getsize /dev/mapper/my_data` snapshot-origin \
56 1.1.2.6 haad * /dev/mapper/my_data_org" | dmsetup load my_data
57 1.1.2.6 haad *
58 1.1.2.6 haad * 8. Resume the snapshot and origin devices.
59 1.1.2.6 haad * dmsetup resume my_data_snap
60 1.1.2.6 haad * dmsetup resume my_data
61 1.1.2.6 haad */
62 1.1.2.6 haad
63 1.1.2.6 haad /*
64 1.1.2.1 haad * This file implements initial version of device-mapper snapshot target.
65 1.1.2.1 haad */
66 1.1.2.1 haad #include <sys/types.h>
67 1.1.2.1 haad #include <sys/param.h>
68 1.1.2.1 haad
69 1.1.2.1 haad #include <sys/buf.h>
70 1.1.2.1 haad #include <sys/errno.h>
71 1.1.2.1 haad #include <sys/ioctl.h>
72 1.1.2.1 haad #include <sys/ioccom.h>
73 1.1.2.1 haad #include <sys/kmem.h>
74 1.1.2.1 haad #include <sys/types.h>
75 1.1.2.1 haad #include <sys/param.h>
76 1.1.2.1 haad #include <sys/queue.h>
77 1.1.2.1 haad
78 1.1.2.1 haad #include "dm.h"
79 1.1.2.1 haad
80 1.1.2.6 haad /*
81 1.1.2.6 haad * Init function called from dm_table_load_ioctl.
82 1.1.2.6 haad * argv: /dev/mapper/my_data_org /dev/mapper/cow_dev p 64
83 1.1.2.6 haad * snapshot_origin device, cow device, persistent flag, chunk size
84 1.1.2.6 haad */
85 1.1.2.1 haad int
86 1.1.2.6 haad dm_target_snapshot_init(struct dm_dev *dmv, void **target_config, char *params)
87 1.1.2.1 haad {
88 1.1.2.6 haad struct target_snapshot_config *tsc;
89 1.1.2.1 haad
90 1.1.2.6 haad char **ap, *argv[5];
91 1.1.2.6 haad
92 1.1.2.6 haad /*
93 1.1.2.6 haad * Parse a string, containing tokens delimited by white space,
94 1.1.2.6 haad * into an argument vector
95 1.1.2.6 haad */
96 1.1.2.6 haad for (ap = argv; ap < &argv[4] &&
97 1.1.2.6 haad (*ap = strsep(¶ms, " \t")) != NULL;) {
98 1.1.2.6 haad if (**ap != '\0')
99 1.1.2.6 haad ap++;
100 1.1.2.6 haad }
101 1.1.2.6 haad
102 1.1.2.1 haad printf("Snapshot target init function called!!\n");
103 1.1.2.6 haad printf("Snapshotted device: %s, cow device %s, persistent flag: %s,"
104 1.1.2.6 haad "chunk size: %s\n", argv[0], argv[1], argv[2], argv[3]);
105 1.1.2.6 haad
106 1.1.2.6 haad if ((tsc = kmem_alloc(sizeof(struct target_snapshot_config), KM_NOSLEEP))
107 1.1.2.6 haad == NULL)
108 1.1.2.6 haad return 1;
109 1.1.2.1 haad
110 1.1.2.6 haad tsc->persistent_dev = 0;
111 1.1.2.6 haad
112 1.1.2.6 haad tsc->chunk_size = atoi(argv[3]);
113 1.1.2.6 haad
114 1.1.2.6 haad if (strcmp(argv[2], "p") == 0)
115 1.1.2.6 haad tsc->persistent_dev = 1;
116 1.1.2.6 haad
117 1.1.2.6 haad *target_config = tsc;
118 1.1.2.1 haad
119 1.1.2.1 haad
120 1.1.2.1 haad dmv->dev_type = DM_SNAPSHOT_DEV;
121 1.1.2.1 haad
122 1.1.2.1 haad return 0;
123 1.1.2.1 haad }
124 1.1.2.1 haad
125 1.1.2.3 haad /*
126 1.1.2.3 haad * Status routine is called to get params string, which is target
127 1.1.2.3 haad * specific. When dm_table_status_ioctl is called with flag
128 1.1.2.3 haad * DM_STATUS_TABLE_FLAG I have to sent params string back.
129 1.1.2.3 haad */
130 1.1.2.3 haad char *
131 1.1.2.3 haad dm_target_snapshot_status(void *target_config)
132 1.1.2.3 haad {
133 1.1.2.3 haad struct target_snapshot_config *tlc;
134 1.1.2.3 haad
135 1.1.2.3 haad tlc = target_config;
136 1.1.2.3 haad
137 1.1.2.3 haad printf("Snapshot target status function called\n");
138 1.1.2.3 haad
139 1.1.2.3 haad return 0;
140 1.1.2.3 haad }
141 1.1.2.3 haad
142 1.1.2.1 haad /* Strategy routine called from dm_strategy. */
143 1.1.2.1 haad int
144 1.1.2.1 haad dm_target_snapshot_strategy(struct dm_table_entry *table_en, struct buf *bp)
145 1.1.2.1 haad {
146 1.1.2.1 haad
147 1.1.2.1 haad printf("Snapshot target read function called!!\n");
148 1.1.2.1 haad
149 1.1.2.1 haad bp->b_error = EIO;
150 1.1.2.1 haad bp->b_resid = 0;
151 1.1.2.1 haad
152 1.1.2.1 haad biodone(bp);
153 1.1.2.1 haad
154 1.1.2.1 haad return 0;
155 1.1.2.1 haad }
156 1.1.2.1 haad
157 1.1.2.1 haad /* Doesn't do anything here. */
158 1.1.2.1 haad int
159 1.1.2.1 haad dm_target_snapshot_destroy(struct dm_table_entry *table_en)
160 1.1.2.1 haad {
161 1.1.2.4 haad table_en->target_config = NULL;
162 1.1.2.4 haad
163 1.1.2.1 haad return 0;
164 1.1.2.1 haad }
165 1.1.2.1 haad
166 1.1.2.1 haad /* Unsupported for this target. */
167 1.1.2.1 haad int
168 1.1.2.1 haad dm_target_snapshot_upcall(struct dm_table_entry *table_en, struct buf *bp)
169 1.1.2.1 haad {
170 1.1.2.1 haad return 0;
171 1.1.2.1 haad }
172 1.1.2.6 haad /*
173 1.1.2.6 haad * dm target snapshot origin routines.
174 1.1.2.6 haad *
175 1.1.2.6 haad * Keep for compatibility with linux lvm2tools. They use two targets
176 1.1.2.6 haad * to implement snapshots. Snapshot target will implement exception
177 1.1.2.6 haad * store and snapshot origin will implement device which calls every
178 1.1.2.6 haad * snapshot device when write is done on master device.
179 1.1.2.6 haad */
180 1.1.2.6 haad
181 1.1.2.6 haad /* Init function called from dm_table_load_ioctl. */
182 1.1.2.6 haad int
183 1.1.2.6 haad dm_target_snapshot_orig_init(struct dm_dev *dmv, void **target_config, char *argv)
184 1.1.2.6 haad {
185 1.1.2.6 haad
186 1.1.2.6 haad printf("Snapshot_Orig target init function called!!\n");
187 1.1.2.6 haad
188 1.1.2.6 haad *target_config = NULL;
189 1.1.2.6 haad
190 1.1.2.6 haad
191 1.1.2.6 haad dmv->dev_type = DM_SNAPSHOT_ORIG_DEV;
192 1.1.2.6 haad
193 1.1.2.6 haad return 0;
194 1.1.2.6 haad }
195 1.1.2.6 haad
196 1.1.2.6 haad /*
197 1.1.2.6 haad * Status routine is called to get params string, which is target
198 1.1.2.6 haad * specific. When dm_table_status_ioctl is called with flag
199 1.1.2.6 haad * DM_STATUS_TABLE_FLAG I have to sent params string back.
200 1.1.2.6 haad */
201 1.1.2.6 haad char *
202 1.1.2.6 haad dm_target_snapshot_orig_status(void *target_config)
203 1.1.2.6 haad {
204 1.1.2.6 haad struct target_snapshot_orig_config *tlc;
205 1.1.2.6 haad
206 1.1.2.6 haad tlc = target_config;
207 1.1.2.6 haad
208 1.1.2.6 haad printf("Snapshot_Orig target status function called\n");
209 1.1.2.6 haad
210 1.1.2.6 haad return 0;
211 1.1.2.6 haad }
212 1.1.2.6 haad
213 1.1.2.6 haad /* Strategy routine called from dm_strategy. */
214 1.1.2.6 haad int
215 1.1.2.6 haad dm_target_snapshot_orig_strategy(struct dm_table_entry *table_en, struct buf *bp)
216 1.1.2.6 haad {
217 1.1.2.6 haad
218 1.1.2.6 haad printf("Snapshot_Orig target read function called!!\n");
219 1.1.2.6 haad
220 1.1.2.6 haad bp->b_error = EIO;
221 1.1.2.6 haad bp->b_resid = 0;
222 1.1.2.6 haad
223 1.1.2.6 haad biodone(bp);
224 1.1.2.6 haad
225 1.1.2.6 haad return 0;
226 1.1.2.6 haad }
227 1.1.2.6 haad
228 1.1.2.6 haad /* Doesn't do anything here. */
229 1.1.2.6 haad int
230 1.1.2.6 haad dm_target_snapshot_orig_destroy(struct dm_table_entry *table_en)
231 1.1.2.6 haad {
232 1.1.2.6 haad table_en->target_config = NULL;
233 1.1.2.6 haad
234 1.1.2.6 haad return 0;
235 1.1.2.6 haad }
236 1.1.2.6 haad
237 1.1.2.6 haad /* Unsupported for this target. */
238 1.1.2.6 haad int
239 1.1.2.6 haad dm_target_snapshot_orig_upcall(struct dm_table_entry *table_en, struct buf *bp)
240 1.1.2.6 haad {
241 1.1.2.6 haad return 0;
242 1.1.2.6 haad }
243