Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 2 | * dm-exception-store.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2001-2002 Sistina Software (UK) Limited. |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 5 | * Copyright (C) 2006 Red Hat GmbH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is released under the GPL. |
| 8 | */ |
| 9 | |
| 10 | #include "dm.h" |
| 11 | #include "dm-snap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/pagemap.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/slab.h> |
Alasdair G Kergon | a765e20 | 2008-04-24 22:02:01 +0100 | [diff] [blame] | 17 | #include <linux/dm-io.h> |
| 18 | #include <linux/dm-kcopyd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 20 | #define DM_MSG_PREFIX "snapshots" |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 21 | #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */ |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /*----------------------------------------------------------------- |
| 24 | * Persistent snapshots, by persistent we mean that the snapshot |
| 25 | * will survive a reboot. |
| 26 | *---------------------------------------------------------------*/ |
| 27 | |
| 28 | /* |
| 29 | * We need to store a record of which parts of the origin have |
| 30 | * been copied to the snapshot device. The snapshot code |
| 31 | * requires that we copy exception chunks to chunk aligned areas |
| 32 | * of the COW store. It makes sense therefore, to store the |
| 33 | * metadata in chunk size blocks. |
| 34 | * |
| 35 | * There is no backward or forward compatibility implemented, |
| 36 | * snapshots with different disk versions than the kernel will |
| 37 | * not be usable. It is expected that "lvcreate" will blank out |
| 38 | * the start of a fresh COW device before calling the snapshot |
| 39 | * constructor. |
| 40 | * |
| 41 | * The first chunk of the COW device just contains the header. |
| 42 | * After this there is a chunk filled with exception metadata, |
| 43 | * followed by as many exception chunks as can fit in the |
| 44 | * metadata areas. |
| 45 | * |
| 46 | * All on disk structures are in little-endian format. The end |
| 47 | * of the exceptions info is indicated by an exception with a |
| 48 | * new_chunk of 0, which is invalid since it would point to the |
| 49 | * header chunk. |
| 50 | */ |
| 51 | |
| 52 | /* |
| 53 | * Magic for persistent snapshots: "SnAp" - Feeble isn't it. |
| 54 | */ |
| 55 | #define SNAP_MAGIC 0x70416e53 |
| 56 | |
| 57 | /* |
| 58 | * The on-disk version of the metadata. |
| 59 | */ |
| 60 | #define SNAPSHOT_DISK_VERSION 1 |
| 61 | |
| 62 | struct disk_header { |
| 63 | uint32_t magic; |
| 64 | |
| 65 | /* |
| 66 | * Is this snapshot valid. There is no way of recovering |
| 67 | * an invalid snapshot. |
| 68 | */ |
| 69 | uint32_t valid; |
| 70 | |
| 71 | /* |
| 72 | * Simple, incrementing version. no backward |
| 73 | * compatibility. |
| 74 | */ |
| 75 | uint32_t version; |
| 76 | |
| 77 | /* In sectors */ |
| 78 | uint32_t chunk_size; |
| 79 | }; |
| 80 | |
| 81 | struct disk_exception { |
| 82 | uint64_t old_chunk; |
| 83 | uint64_t new_chunk; |
| 84 | }; |
| 85 | |
| 86 | struct commit_callback { |
| 87 | void (*callback)(void *, int success); |
| 88 | void *context; |
| 89 | }; |
| 90 | |
| 91 | /* |
| 92 | * The top level structure for a persistent exception store. |
| 93 | */ |
| 94 | struct pstore { |
| 95 | struct dm_snapshot *snap; /* up pointer to my snapshot */ |
| 96 | int version; |
| 97 | int valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | uint32_t exceptions_per_area; |
| 99 | |
| 100 | /* |
| 101 | * Now that we have an asynchronous kcopyd there is no |
| 102 | * need for large chunk sizes, so it wont hurt to have a |
| 103 | * whole chunks worth of metadata in memory at once. |
| 104 | */ |
| 105 | void *area; |
| 106 | |
| 107 | /* |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 108 | * An area of zeros used to clear the next area. |
| 109 | */ |
| 110 | void *zero_area; |
| 111 | |
| 112 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * Used to keep track of which metadata area the data in |
| 114 | * 'chunk' refers to. |
| 115 | */ |
Mikulas Patocka | fd14acf | 2008-10-10 13:37:01 +0100 | [diff] [blame] | 116 | chunk_t current_area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * The next free chunk for an exception. |
| 120 | */ |
Mikulas Patocka | fd14acf | 2008-10-10 13:37:01 +0100 | [diff] [blame] | 121 | chunk_t next_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * The index of next free exception in the current |
| 125 | * metadata area. |
| 126 | */ |
| 127 | uint32_t current_committed; |
| 128 | |
| 129 | atomic_t pending_count; |
| 130 | uint32_t callback_count; |
| 131 | struct commit_callback *callbacks; |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 132 | struct dm_io_client *io_client; |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 133 | |
| 134 | struct workqueue_struct *metadata_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 137 | static unsigned sectors_to_pages(unsigned sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Mikulas Patocka | 9243626 | 2008-04-24 21:41:50 +0100 | [diff] [blame] | 139 | return DIV_ROUND_UP(sectors, PAGE_SIZE >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int alloc_area(struct pstore *ps) |
| 143 | { |
| 144 | int r = -ENOMEM; |
| 145 | size_t len; |
| 146 | |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame] | 147 | len = ps->snap->chunk_size << SECTOR_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Allocate the chunk_size block of memory that will hold |
| 151 | * a single metadata area. |
| 152 | */ |
| 153 | ps->area = vmalloc(len); |
| 154 | if (!ps->area) |
| 155 | return r; |
| 156 | |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 157 | ps->zero_area = vmalloc(len); |
| 158 | if (!ps->zero_area) { |
| 159 | vfree(ps->area); |
| 160 | return r; |
| 161 | } |
| 162 | memset(ps->zero_area, 0, len); |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static void free_area(struct pstore *ps) |
| 168 | { |
| 169 | vfree(ps->area); |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 170 | ps->area = NULL; |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 171 | vfree(ps->zero_area); |
| 172 | ps->zero_area = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 175 | struct mdata_req { |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 176 | struct dm_io_region *where; |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 177 | struct dm_io_request *io_req; |
| 178 | struct work_struct work; |
| 179 | int result; |
| 180 | }; |
| 181 | |
| 182 | static void do_metadata(struct work_struct *work) |
| 183 | { |
| 184 | struct mdata_req *req = container_of(work, struct mdata_req, work); |
| 185 | |
| 186 | req->result = dm_io(req->io_req, 1, req->where, NULL); |
| 187 | } |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Read or write a chunk aligned and sized block of data from a device. |
| 191 | */ |
Mikulas Patocka | fd14acf | 2008-10-10 13:37:01 +0100 | [diff] [blame] | 192 | static int chunk_io(struct pstore *ps, chunk_t chunk, int rw, int metadata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 194 | struct dm_io_region where = { |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 195 | .bdev = ps->snap->cow->bdev, |
| 196 | .sector = ps->snap->chunk_size * chunk, |
| 197 | .count = ps->snap->chunk_size, |
| 198 | }; |
| 199 | struct dm_io_request io_req = { |
| 200 | .bi_rw = rw, |
| 201 | .mem.type = DM_IO_VMA, |
| 202 | .mem.ptr.vma = ps->area, |
| 203 | .client = ps->io_client, |
| 204 | .notify.fn = NULL, |
| 205 | }; |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 206 | struct mdata_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 208 | if (!metadata) |
| 209 | return dm_io(&io_req, 1, &where, NULL); |
| 210 | |
| 211 | req.where = &where; |
| 212 | req.io_req = &io_req; |
| 213 | |
| 214 | /* |
| 215 | * Issue the synchronous I/O from a different thread |
| 216 | * to avoid generic_make_request recursion. |
| 217 | */ |
| 218 | INIT_WORK(&req.work, do_metadata); |
| 219 | queue_work(ps->metadata_wq, &req.work); |
| 220 | flush_workqueue(ps->metadata_wq); |
| 221 | |
| 222 | return req.result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
Mikulas Patocka | a481db7 | 2008-10-10 13:37:00 +0100 | [diff] [blame] | 226 | * Convert a metadata area index to a chunk index. |
| 227 | */ |
| 228 | static chunk_t area_location(struct pstore *ps, chunk_t area) |
| 229 | { |
| 230 | return 1 + ((ps->exceptions_per_area + 1) * area); |
| 231 | } |
| 232 | |
| 233 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | * Read or write a metadata area. Remembering to skip the first |
| 235 | * chunk which holds the header. |
| 236 | */ |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 237 | static int area_io(struct pstore *ps, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | int r; |
Mikulas Patocka | fd14acf | 2008-10-10 13:37:01 +0100 | [diff] [blame] | 240 | chunk_t chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 242 | chunk = area_location(ps, ps->current_area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 244 | r = chunk_io(ps, chunk, rw, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (r) |
| 246 | return r; |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 251 | static void zero_memory_area(struct pstore *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame] | 253 | memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT); |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 254 | } |
| 255 | |
| 256 | static int zero_disk_area(struct pstore *ps, chunk_t area) |
| 257 | { |
| 258 | struct dm_io_region where = { |
| 259 | .bdev = ps->snap->cow->bdev, |
| 260 | .sector = ps->snap->chunk_size * area_location(ps, area), |
| 261 | .count = ps->snap->chunk_size, |
| 262 | }; |
| 263 | struct dm_io_request io_req = { |
| 264 | .bi_rw = WRITE, |
| 265 | .mem.type = DM_IO_VMA, |
| 266 | .mem.ptr.vma = ps->zero_area, |
| 267 | .client = ps->io_client, |
| 268 | .notify.fn = NULL, |
| 269 | }; |
| 270 | |
| 271 | return dm_io(&io_req, 1, &where, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static int read_header(struct pstore *ps, int *new_snapshot) |
| 275 | { |
| 276 | int r; |
| 277 | struct disk_header *dh; |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame] | 278 | chunk_t chunk_size; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 279 | int chunk_size_supplied = 1; |
| 280 | |
| 281 | /* |
| 282 | * Use default chunk size (or hardsect_size, if larger) if none supplied |
| 283 | */ |
| 284 | if (!ps->snap->chunk_size) { |
| 285 | ps->snap->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS, |
| 286 | bdev_hardsect_size(ps->snap->cow->bdev) >> 9); |
| 287 | ps->snap->chunk_mask = ps->snap->chunk_size - 1; |
| 288 | ps->snap->chunk_shift = ffs(ps->snap->chunk_size) - 1; |
| 289 | chunk_size_supplied = 0; |
| 290 | } |
| 291 | |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 292 | ps->io_client = dm_io_client_create(sectors_to_pages(ps->snap-> |
| 293 | chunk_size)); |
| 294 | if (IS_ERR(ps->io_client)) |
| 295 | return PTR_ERR(ps->io_client); |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 296 | |
| 297 | r = alloc_area(ps); |
| 298 | if (r) |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 299 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 301 | r = chunk_io(ps, 0, READ, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (r) |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 303 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | dh = (struct disk_header *) ps->area; |
| 306 | |
| 307 | if (le32_to_cpu(dh->magic) == 0) { |
| 308 | *new_snapshot = 1; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 309 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 312 | if (le32_to_cpu(dh->magic) != SNAP_MAGIC) { |
| 313 | DMWARN("Invalid or corrupt snapshot"); |
| 314 | r = -ENXIO; |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 315 | goto bad; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | *new_snapshot = 0; |
| 319 | ps->valid = le32_to_cpu(dh->valid); |
| 320 | ps->version = le32_to_cpu(dh->version); |
| 321 | chunk_size = le32_to_cpu(dh->chunk_size); |
| 322 | |
| 323 | if (!chunk_size_supplied || ps->snap->chunk_size == chunk_size) |
| 324 | return 0; |
| 325 | |
| 326 | DMWARN("chunk size %llu in device metadata overrides " |
| 327 | "table chunk size of %llu.", |
| 328 | (unsigned long long)chunk_size, |
| 329 | (unsigned long long)ps->snap->chunk_size); |
| 330 | |
| 331 | /* We had a bogus chunk_size. Fix stuff up. */ |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 332 | free_area(ps); |
| 333 | |
| 334 | ps->snap->chunk_size = chunk_size; |
| 335 | ps->snap->chunk_mask = chunk_size - 1; |
| 336 | ps->snap->chunk_shift = ffs(chunk_size) - 1; |
| 337 | |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 338 | r = dm_io_client_resize(sectors_to_pages(ps->snap->chunk_size), |
| 339 | ps->io_client); |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 340 | if (r) |
| 341 | return r; |
| 342 | |
| 343 | r = alloc_area(ps); |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 344 | return r; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 345 | |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 346 | bad: |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 347 | free_area(ps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return r; |
| 349 | } |
| 350 | |
| 351 | static int write_header(struct pstore *ps) |
| 352 | { |
| 353 | struct disk_header *dh; |
| 354 | |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame] | 355 | memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | dh = (struct disk_header *) ps->area; |
| 358 | dh->magic = cpu_to_le32(SNAP_MAGIC); |
| 359 | dh->valid = cpu_to_le32(ps->valid); |
| 360 | dh->version = cpu_to_le32(ps->version); |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame] | 361 | dh->chunk_size = cpu_to_le32(ps->snap->chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 363 | return chunk_io(ps, 0, WRITE, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Access functions for the disk exceptions, these do the endian conversions. |
| 368 | */ |
| 369 | static struct disk_exception *get_exception(struct pstore *ps, uint32_t index) |
| 370 | { |
Mark McLoughlin | e4ff496 | 2006-10-03 01:15:26 -0700 | [diff] [blame] | 371 | BUG_ON(index >= ps->exceptions_per_area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | return ((struct disk_exception *) ps->area) + index; |
| 374 | } |
| 375 | |
Mark McLoughlin | e4ff496 | 2006-10-03 01:15:26 -0700 | [diff] [blame] | 376 | static void read_exception(struct pstore *ps, |
| 377 | uint32_t index, struct disk_exception *result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
Mark McLoughlin | e4ff496 | 2006-10-03 01:15:26 -0700 | [diff] [blame] | 379 | struct disk_exception *e = get_exception(ps, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | /* copy it */ |
| 382 | result->old_chunk = le64_to_cpu(e->old_chunk); |
| 383 | result->new_chunk = le64_to_cpu(e->new_chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Mark McLoughlin | e4ff496 | 2006-10-03 01:15:26 -0700 | [diff] [blame] | 386 | static void write_exception(struct pstore *ps, |
| 387 | uint32_t index, struct disk_exception *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
Mark McLoughlin | e4ff496 | 2006-10-03 01:15:26 -0700 | [diff] [blame] | 389 | struct disk_exception *e = get_exception(ps, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | /* copy it */ |
| 392 | e->old_chunk = cpu_to_le64(de->old_chunk); |
| 393 | e->new_chunk = cpu_to_le64(de->new_chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Registers the exceptions that are present in the current area. |
| 398 | * 'full' is filled in to indicate if the area has been |
| 399 | * filled. |
| 400 | */ |
| 401 | static int insert_exceptions(struct pstore *ps, int *full) |
| 402 | { |
| 403 | int r; |
| 404 | unsigned int i; |
| 405 | struct disk_exception de; |
| 406 | |
| 407 | /* presume the area is full */ |
| 408 | *full = 1; |
| 409 | |
| 410 | for (i = 0; i < ps->exceptions_per_area; i++) { |
Mark McLoughlin | e4ff496 | 2006-10-03 01:15:26 -0700 | [diff] [blame] | 411 | read_exception(ps, i, &de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * If the new_chunk is pointing at the start of |
| 415 | * the COW device, where the first metadata area |
| 416 | * is we know that we've hit the end of the |
| 417 | * exceptions. Therefore the area is not full. |
| 418 | */ |
| 419 | if (de.new_chunk == 0LL) { |
| 420 | ps->current_committed = i; |
| 421 | *full = 0; |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Keep track of the start of the free chunks. |
| 427 | */ |
| 428 | if (ps->next_free <= de.new_chunk) |
| 429 | ps->next_free = de.new_chunk + 1; |
| 430 | |
| 431 | /* |
| 432 | * Otherwise we add the exception to the snapshot. |
| 433 | */ |
| 434 | r = dm_add_exception(ps->snap, de.old_chunk, de.new_chunk); |
| 435 | if (r) |
| 436 | return r; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int read_exceptions(struct pstore *ps) |
| 443 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | int r, full = 1; |
| 445 | |
| 446 | /* |
| 447 | * Keeping reading chunks and inserting exceptions until |
| 448 | * we find a partially full area. |
| 449 | */ |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 450 | for (ps->current_area = 0; full; ps->current_area++) { |
| 451 | r = area_io(ps, READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (r) |
| 453 | return r; |
| 454 | |
| 455 | r = insert_exceptions(ps, &full); |
| 456 | if (r) |
| 457 | return r; |
| 458 | } |
| 459 | |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 460 | ps->current_area--; |
| 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 465 | static struct pstore *get_info(struct exception_store *store) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
| 467 | return (struct pstore *) store->context; |
| 468 | } |
| 469 | |
| 470 | static void persistent_fraction_full(struct exception_store *store, |
| 471 | sector_t *numerator, sector_t *denominator) |
| 472 | { |
| 473 | *numerator = get_info(store)->next_free * store->snap->chunk_size; |
| 474 | *denominator = get_dev_size(store->snap->cow->bdev); |
| 475 | } |
| 476 | |
| 477 | static void persistent_destroy(struct exception_store *store) |
| 478 | { |
| 479 | struct pstore *ps = get_info(store); |
| 480 | |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 481 | destroy_workqueue(ps->metadata_wq); |
Heinz Mauelshagen | 6cca1e7 | 2007-05-09 02:33:02 -0700 | [diff] [blame] | 482 | dm_io_client_destroy(ps->io_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | vfree(ps->callbacks); |
| 484 | free_area(ps); |
| 485 | kfree(ps); |
| 486 | } |
| 487 | |
| 488 | static int persistent_read_metadata(struct exception_store *store) |
| 489 | { |
Andrew Morton | e48b9db | 2008-02-08 02:10:11 +0000 | [diff] [blame] | 490 | int r, uninitialized_var(new_snapshot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | struct pstore *ps = get_info(store); |
| 492 | |
| 493 | /* |
| 494 | * Read the snapshot header. |
| 495 | */ |
| 496 | r = read_header(ps, &new_snapshot); |
| 497 | if (r) |
| 498 | return r; |
| 499 | |
| 500 | /* |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame] | 501 | * Now we know correct chunk_size, complete the initialisation. |
| 502 | */ |
| 503 | ps->exceptions_per_area = (ps->snap->chunk_size << SECTOR_SHIFT) / |
| 504 | sizeof(struct disk_exception); |
| 505 | ps->callbacks = dm_vcalloc(ps->exceptions_per_area, |
| 506 | sizeof(*ps->callbacks)); |
| 507 | if (!ps->callbacks) |
| 508 | return -ENOMEM; |
| 509 | |
| 510 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | * Do we need to setup a new snapshot ? |
| 512 | */ |
| 513 | if (new_snapshot) { |
| 514 | r = write_header(ps); |
| 515 | if (r) { |
| 516 | DMWARN("write_header failed"); |
| 517 | return r; |
| 518 | } |
| 519 | |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 520 | ps->current_area = 0; |
| 521 | zero_memory_area(ps); |
| 522 | r = zero_disk_area(ps, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (r) { |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 524 | DMWARN("zero_disk_area(0) failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return r; |
| 526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } else { |
| 528 | /* |
| 529 | * Sanity checks. |
| 530 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (ps->version != SNAPSHOT_DISK_VERSION) { |
| 532 | DMWARN("unable to handle snapshot disk version %d", |
| 533 | ps->version); |
| 534 | return -EINVAL; |
| 535 | } |
| 536 | |
| 537 | /* |
Milan Broz | 0764147 | 2007-07-12 17:28:13 +0100 | [diff] [blame] | 538 | * Metadata are valid, but snapshot is invalidated |
| 539 | */ |
| 540 | if (!ps->valid) |
| 541 | return 1; |
| 542 | |
| 543 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | * Read the metadata. |
| 545 | */ |
| 546 | r = read_exceptions(ps); |
| 547 | if (r) |
| 548 | return r; |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int persistent_prepare(struct exception_store *store, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 555 | struct dm_snap_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
| 557 | struct pstore *ps = get_info(store); |
| 558 | uint32_t stride; |
Mikulas Patocka | fd14acf | 2008-10-10 13:37:01 +0100 | [diff] [blame] | 559 | chunk_t next_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | sector_t size = get_dev_size(store->snap->cow->bdev); |
| 561 | |
| 562 | /* Is there enough room ? */ |
| 563 | if (size < ((ps->next_free + 1) * store->snap->chunk_size)) |
| 564 | return -ENOSPC; |
| 565 | |
| 566 | e->new_chunk = ps->next_free; |
| 567 | |
| 568 | /* |
| 569 | * Move onto the next free pending, making sure to take |
| 570 | * into account the location of the metadata chunks. |
| 571 | */ |
| 572 | stride = (ps->exceptions_per_area + 1); |
Mikulas Patocka | fd14acf | 2008-10-10 13:37:01 +0100 | [diff] [blame] | 573 | next_free = ++ps->next_free; |
| 574 | if (sector_div(next_free, stride) == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | ps->next_free++; |
| 576 | |
| 577 | atomic_inc(&ps->pending_count); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static void persistent_commit(struct exception_store *store, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 582 | struct dm_snap_exception *e, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | void (*callback) (void *, int success), |
| 584 | void *callback_context) |
| 585 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | unsigned int i; |
| 587 | struct pstore *ps = get_info(store); |
| 588 | struct disk_exception de; |
| 589 | struct commit_callback *cb; |
| 590 | |
| 591 | de.old_chunk = e->old_chunk; |
| 592 | de.new_chunk = e->new_chunk; |
| 593 | write_exception(ps, ps->current_committed++, &de); |
| 594 | |
| 595 | /* |
| 596 | * Add the callback to the back of the array. This code |
| 597 | * is the only place where the callback array is |
| 598 | * manipulated, and we know that it will never be called |
| 599 | * multiple times concurrently. |
| 600 | */ |
| 601 | cb = ps->callbacks + ps->callback_count++; |
| 602 | cb->callback = callback; |
| 603 | cb->context = callback_context; |
| 604 | |
| 605 | /* |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 606 | * If there are exceptions in flight and we have not yet |
| 607 | * filled this metadata area there's nothing more to do. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | */ |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 609 | if (!atomic_dec_and_test(&ps->pending_count) && |
| 610 | (ps->current_committed != ps->exceptions_per_area)) |
| 611 | return; |
| 612 | |
| 613 | /* |
| 614 | * Commit exceptions to disk. |
| 615 | */ |
| 616 | if (area_io(ps, WRITE)) |
| 617 | ps->valid = 0; |
| 618 | |
| 619 | /* |
| 620 | * Advance to the next area if this one is full. |
| 621 | */ |
| 622 | if (ps->current_committed == ps->exceptions_per_area) { |
| 623 | if (zero_disk_area(ps, ps->current_area + 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | ps->valid = 0; |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 625 | ps->current_committed = 0; |
| 626 | ps->current_area++; |
| 627 | zero_memory_area(ps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
Alasdair G Kergon | 7c9e6c1 | 2008-10-21 17:44:55 +0100 | [diff] [blame^] | 629 | |
| 630 | for (i = 0; i < ps->callback_count; i++) { |
| 631 | cb = ps->callbacks + i; |
| 632 | cb->callback(cb->context, ps->valid); |
| 633 | } |
| 634 | |
| 635 | ps->callback_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static void persistent_drop(struct exception_store *store) |
| 639 | { |
| 640 | struct pstore *ps = get_info(store); |
| 641 | |
| 642 | ps->valid = 0; |
| 643 | if (write_header(ps)) |
| 644 | DMWARN("write header failed"); |
| 645 | } |
| 646 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 647 | int dm_create_persistent(struct exception_store *store) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | struct pstore *ps; |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* allocate the pstore */ |
| 652 | ps = kmalloc(sizeof(*ps), GFP_KERNEL); |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 653 | if (!ps) |
| 654 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | ps->snap = store->snap; |
| 657 | ps->valid = 1; |
| 658 | ps->version = SNAPSHOT_DISK_VERSION; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 659 | ps->area = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | ps->next_free = 2; /* skipping the header and first area */ |
| 661 | ps->current_committed = 0; |
| 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | ps->callback_count = 0; |
| 664 | atomic_set(&ps->pending_count, 0); |
Alasdair G Kergon | c51c275 | 2006-06-26 00:27:18 -0700 | [diff] [blame] | 665 | ps->callbacks = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 667 | ps->metadata_wq = create_singlethread_workqueue("ksnaphd"); |
| 668 | if (!ps->metadata_wq) { |
Jesper Juhl | 851a8a7 | 2007-07-18 00:49:08 -0700 | [diff] [blame] | 669 | kfree(ps); |
Milan Broz | fcac03a | 2007-07-12 17:28:00 +0100 | [diff] [blame] | 670 | DMERR("couldn't start header metadata update thread"); |
| 671 | return -ENOMEM; |
| 672 | } |
| 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | store->destroy = persistent_destroy; |
| 675 | store->read_metadata = persistent_read_metadata; |
| 676 | store->prepare_exception = persistent_prepare; |
| 677 | store->commit_exception = persistent_commit; |
| 678 | store->drop_snapshot = persistent_drop; |
| 679 | store->fraction_full = persistent_fraction_full; |
| 680 | store->context = ps; |
| 681 | |
| 682 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /*----------------------------------------------------------------- |
| 686 | * Implementation of the store for non-persistent snapshots. |
| 687 | *---------------------------------------------------------------*/ |
| 688 | struct transient_c { |
| 689 | sector_t next_free; |
| 690 | }; |
| 691 | |
| 692 | static void transient_destroy(struct exception_store *store) |
| 693 | { |
| 694 | kfree(store->context); |
| 695 | } |
| 696 | |
| 697 | static int transient_read_metadata(struct exception_store *store) |
| 698 | { |
| 699 | return 0; |
| 700 | } |
| 701 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 702 | static int transient_prepare(struct exception_store *store, |
| 703 | struct dm_snap_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
| 705 | struct transient_c *tc = (struct transient_c *) store->context; |
| 706 | sector_t size = get_dev_size(store->snap->cow->bdev); |
| 707 | |
| 708 | if (size < (tc->next_free + store->snap->chunk_size)) |
| 709 | return -1; |
| 710 | |
| 711 | e->new_chunk = sector_to_chunk(store->snap, tc->next_free); |
| 712 | tc->next_free += store->snap->chunk_size; |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static void transient_commit(struct exception_store *store, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 718 | struct dm_snap_exception *e, |
| 719 | void (*callback) (void *, int success), |
| 720 | void *callback_context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
| 722 | /* Just succeed */ |
| 723 | callback(callback_context, 1); |
| 724 | } |
| 725 | |
| 726 | static void transient_fraction_full(struct exception_store *store, |
| 727 | sector_t *numerator, sector_t *denominator) |
| 728 | { |
| 729 | *numerator = ((struct transient_c *) store->context)->next_free; |
| 730 | *denominator = get_dev_size(store->snap->cow->bdev); |
| 731 | } |
| 732 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 733 | int dm_create_transient(struct exception_store *store) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
| 735 | struct transient_c *tc; |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | store->destroy = transient_destroy; |
| 738 | store->read_metadata = transient_read_metadata; |
| 739 | store->prepare_exception = transient_prepare; |
| 740 | store->commit_exception = transient_commit; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 741 | store->drop_snapshot = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | store->fraction_full = transient_fraction_full; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL); |
| 745 | if (!tc) |
| 746 | return -ENOMEM; |
| 747 | |
| 748 | tc->next_free = 0; |
| 749 | store->context = tc; |
| 750 | |
| 751 | return 0; |
| 752 | } |