blob: 767065f6c5f30b8b6d5e93ffb9ce5a98b18455d0 [file] [log] [blame]
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001/*
2 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2006-2008 Red Hat GmbH
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm-exception-store.h"
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00009
10#include <linux/mm.h>
11#include <linux/pagemap.h>
12#include <linux/vmalloc.h>
13#include <linux/slab.h>
14#include <linux/dm-io.h>
15
16#define DM_MSG_PREFIX "persistent snapshot"
17#define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
18
19/*-----------------------------------------------------------------
20 * Persistent snapshots, by persistent we mean that the snapshot
21 * will survive a reboot.
22 *---------------------------------------------------------------*/
23
24/*
25 * We need to store a record of which parts of the origin have
26 * been copied to the snapshot device. The snapshot code
27 * requires that we copy exception chunks to chunk aligned areas
28 * of the COW store. It makes sense therefore, to store the
29 * metadata in chunk size blocks.
30 *
31 * There is no backward or forward compatibility implemented,
32 * snapshots with different disk versions than the kernel will
33 * not be usable. It is expected that "lvcreate" will blank out
34 * the start of a fresh COW device before calling the snapshot
35 * constructor.
36 *
37 * The first chunk of the COW device just contains the header.
38 * After this there is a chunk filled with exception metadata,
39 * followed by as many exception chunks as can fit in the
40 * metadata areas.
41 *
42 * All on disk structures are in little-endian format. The end
43 * of the exceptions info is indicated by an exception with a
44 * new_chunk of 0, which is invalid since it would point to the
45 * header chunk.
46 */
47
48/*
49 * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
50 */
51#define SNAP_MAGIC 0x70416e53
52
53/*
54 * The on-disk version of the metadata.
55 */
56#define SNAPSHOT_DISK_VERSION 1
57
58struct disk_header {
59 uint32_t magic;
60
61 /*
62 * Is this snapshot valid. There is no way of recovering
63 * an invalid snapshot.
64 */
65 uint32_t valid;
66
67 /*
68 * Simple, incrementing version. no backward
69 * compatibility.
70 */
71 uint32_t version;
72
73 /* In sectors */
74 uint32_t chunk_size;
75};
76
77struct disk_exception {
78 uint64_t old_chunk;
79 uint64_t new_chunk;
80};
81
82struct commit_callback {
83 void (*callback)(void *, int success);
84 void *context;
85};
86
87/*
88 * The top level structure for a persistent exception store.
89 */
90struct pstore {
Jonathan Brassow71fab002009-04-02 19:55:33 +010091 struct dm_exception_store *store;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +000092 int version;
93 int valid;
94 uint32_t exceptions_per_area;
95
96 /*
97 * Now that we have an asynchronous kcopyd there is no
98 * need for large chunk sizes, so it wont hurt to have a
99 * whole chunks worth of metadata in memory at once.
100 */
101 void *area;
102
103 /*
104 * An area of zeros used to clear the next area.
105 */
106 void *zero_area;
107
108 /*
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100109 * An area used for header. The header can be written
110 * concurrently with metadata (when invalidating the snapshot),
111 * so it needs a separate buffer.
112 */
113 void *header_area;
114
115 /*
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000116 * Used to keep track of which metadata area the data in
117 * 'chunk' refers to.
118 */
119 chunk_t current_area;
120
121 /*
122 * The next free chunk for an exception.
123 */
124 chunk_t next_free;
125
126 /*
127 * The index of next free exception in the current
128 * metadata area.
129 */
130 uint32_t current_committed;
131
132 atomic_t pending_count;
133 uint32_t callback_count;
134 struct commit_callback *callbacks;
135 struct dm_io_client *io_client;
136
137 struct workqueue_struct *metadata_wq;
138};
139
140static unsigned sectors_to_pages(unsigned sectors)
141{
142 return DIV_ROUND_UP(sectors, PAGE_SIZE >> 9);
143}
144
145static int alloc_area(struct pstore *ps)
146{
147 int r = -ENOMEM;
148 size_t len;
149
Jonathan Brassow71fab002009-04-02 19:55:33 +0100150 len = ps->store->chunk_size << SECTOR_SHIFT;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000151
152 /*
153 * Allocate the chunk_size block of memory that will hold
154 * a single metadata area.
155 */
156 ps->area = vmalloc(len);
157 if (!ps->area)
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100158 goto err_area;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000159
160 ps->zero_area = vmalloc(len);
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100161 if (!ps->zero_area)
162 goto err_zero_area;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000163 memset(ps->zero_area, 0, len);
164
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100165 ps->header_area = vmalloc(len);
166 if (!ps->header_area)
167 goto err_header_area;
168
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000169 return 0;
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100170
171err_header_area:
172 vfree(ps->zero_area);
173
174err_zero_area:
175 vfree(ps->area);
176
177err_area:
178 return r;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000179}
180
181static void free_area(struct pstore *ps)
182{
Jonathan Brassowa32079c2009-04-02 19:55:35 +0100183 if (ps->area)
184 vfree(ps->area);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000185 ps->area = NULL;
Jonathan Brassowa32079c2009-04-02 19:55:35 +0100186
187 if (ps->zero_area)
188 vfree(ps->zero_area);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000189 ps->zero_area = NULL;
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100190
191 if (ps->header_area)
192 vfree(ps->header_area);
193 ps->header_area = NULL;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000194}
195
196struct mdata_req {
197 struct dm_io_region *where;
198 struct dm_io_request *io_req;
199 struct work_struct work;
200 int result;
201};
202
203static void do_metadata(struct work_struct *work)
204{
205 struct mdata_req *req = container_of(work, struct mdata_req, work);
206
207 req->result = dm_io(req->io_req, 1, req->where, NULL);
208}
209
210/*
211 * Read or write a chunk aligned and sized block of data from a device.
212 */
Mikulas Patocka02d2fd32009-09-04 20:40:37 +0100213static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, int rw,
214 int metadata)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000215{
216 struct dm_io_region where = {
Jonathan Brassow71fab002009-04-02 19:55:33 +0100217 .bdev = ps->store->cow->bdev,
218 .sector = ps->store->chunk_size * chunk,
219 .count = ps->store->chunk_size,
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000220 };
221 struct dm_io_request io_req = {
222 .bi_rw = rw,
223 .mem.type = DM_IO_VMA,
Mikulas Patocka02d2fd32009-09-04 20:40:37 +0100224 .mem.ptr.vma = area,
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000225 .client = ps->io_client,
226 .notify.fn = NULL,
227 };
228 struct mdata_req req;
229
230 if (!metadata)
231 return dm_io(&io_req, 1, &where, NULL);
232
233 req.where = &where;
234 req.io_req = &io_req;
235
236 /*
237 * Issue the synchronous I/O from a different thread
238 * to avoid generic_make_request recursion.
239 */
240 INIT_WORK(&req.work, do_metadata);
241 queue_work(ps->metadata_wq, &req.work);
242 flush_workqueue(ps->metadata_wq);
243
244 return req.result;
245}
246
247/*
248 * Convert a metadata area index to a chunk index.
249 */
250static chunk_t area_location(struct pstore *ps, chunk_t area)
251{
252 return 1 + ((ps->exceptions_per_area + 1) * area);
253}
254
255/*
256 * Read or write a metadata area. Remembering to skip the first
257 * chunk which holds the header.
258 */
259static int area_io(struct pstore *ps, int rw)
260{
261 int r;
262 chunk_t chunk;
263
264 chunk = area_location(ps, ps->current_area);
265
Mikulas Patocka02d2fd32009-09-04 20:40:37 +0100266 r = chunk_io(ps, ps->area, chunk, rw, 0);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000267 if (r)
268 return r;
269
270 return 0;
271}
272
273static void zero_memory_area(struct pstore *ps)
274{
Jonathan Brassow71fab002009-04-02 19:55:33 +0100275 memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000276}
277
278static int zero_disk_area(struct pstore *ps, chunk_t area)
279{
Mikulas Patocka02d2fd32009-09-04 20:40:37 +0100280 return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000281}
282
283static int read_header(struct pstore *ps, int *new_snapshot)
284{
285 int r;
286 struct disk_header *dh;
Mikulas Patockadf96eee2009-10-16 23:18:17 +0100287 unsigned chunk_size;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000288 int chunk_size_supplied = 1;
Mikulas Patockaae0b7442009-09-04 20:40:43 +0100289 char *chunk_err;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000290
291 /*
Mikulas Patockadf96eee2009-10-16 23:18:17 +0100292 * Use default chunk size (or logical_block_size, if larger)
293 * if none supplied
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000294 */
Jonathan Brassow71fab002009-04-02 19:55:33 +0100295 if (!ps->store->chunk_size) {
296 ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400297 bdev_logical_block_size(ps->store->cow->bdev) >> 9);
Jonathan Brassow71fab002009-04-02 19:55:33 +0100298 ps->store->chunk_mask = ps->store->chunk_size - 1;
299 ps->store->chunk_shift = ffs(ps->store->chunk_size) - 1;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000300 chunk_size_supplied = 0;
301 }
302
Jonathan Brassow71fab002009-04-02 19:55:33 +0100303 ps->io_client = dm_io_client_create(sectors_to_pages(ps->store->
304 chunk_size));
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000305 if (IS_ERR(ps->io_client))
306 return PTR_ERR(ps->io_client);
307
308 r = alloc_area(ps);
309 if (r)
310 return r;
311
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100312 r = chunk_io(ps, ps->header_area, 0, READ, 1);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000313 if (r)
314 goto bad;
315
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100316 dh = ps->header_area;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000317
318 if (le32_to_cpu(dh->magic) == 0) {
319 *new_snapshot = 1;
320 return 0;
321 }
322
323 if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
324 DMWARN("Invalid or corrupt snapshot");
325 r = -ENXIO;
326 goto bad;
327 }
328
329 *new_snapshot = 0;
330 ps->valid = le32_to_cpu(dh->valid);
331 ps->version = le32_to_cpu(dh->version);
332 chunk_size = le32_to_cpu(dh->chunk_size);
333
Mikulas Patockaae0b7442009-09-04 20:40:43 +0100334 if (ps->store->chunk_size == chunk_size)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000335 return 0;
336
Mikulas Patockaae0b7442009-09-04 20:40:43 +0100337 if (chunk_size_supplied)
Mikulas Patockadf96eee2009-10-16 23:18:17 +0100338 DMWARN("chunk size %u in device metadata overrides "
339 "table chunk size of %u.",
340 chunk_size, ps->store->chunk_size);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000341
342 /* We had a bogus chunk_size. Fix stuff up. */
343 free_area(ps);
344
Mikulas Patockaae0b7442009-09-04 20:40:43 +0100345 r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
346 &chunk_err);
347 if (r) {
Mikulas Patockadf96eee2009-10-16 23:18:17 +0100348 DMERR("invalid on-disk chunk size %u: %s.",
349 chunk_size, chunk_err);
Mikulas Patockaae0b7442009-09-04 20:40:43 +0100350 return r;
351 }
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000352
Jonathan Brassow71fab002009-04-02 19:55:33 +0100353 r = dm_io_client_resize(sectors_to_pages(ps->store->chunk_size),
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000354 ps->io_client);
355 if (r)
356 return r;
357
358 r = alloc_area(ps);
359 return r;
360
361bad:
362 free_area(ps);
363 return r;
364}
365
366static int write_header(struct pstore *ps)
367{
368 struct disk_header *dh;
369
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100370 memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000371
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100372 dh = ps->header_area;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000373 dh->magic = cpu_to_le32(SNAP_MAGIC);
374 dh->valid = cpu_to_le32(ps->valid);
375 dh->version = cpu_to_le32(ps->version);
Jonathan Brassow71fab002009-04-02 19:55:33 +0100376 dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000377
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100378 return chunk_io(ps, ps->header_area, 0, WRITE, 1);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000379}
380
381/*
382 * Access functions for the disk exceptions, these do the endian conversions.
383 */
384static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
385{
386 BUG_ON(index >= ps->exceptions_per_area);
387
388 return ((struct disk_exception *) ps->area) + index;
389}
390
391static void read_exception(struct pstore *ps,
392 uint32_t index, struct disk_exception *result)
393{
394 struct disk_exception *e = get_exception(ps, index);
395
396 /* copy it */
397 result->old_chunk = le64_to_cpu(e->old_chunk);
398 result->new_chunk = le64_to_cpu(e->new_chunk);
399}
400
401static void write_exception(struct pstore *ps,
402 uint32_t index, struct disk_exception *de)
403{
404 struct disk_exception *e = get_exception(ps, index);
405
406 /* copy it */
407 e->old_chunk = cpu_to_le64(de->old_chunk);
408 e->new_chunk = cpu_to_le64(de->new_chunk);
409}
410
411/*
412 * Registers the exceptions that are present in the current area.
413 * 'full' is filled in to indicate if the area has been
414 * filled.
415 */
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000416static int insert_exceptions(struct pstore *ps,
417 int (*callback)(void *callback_context,
418 chunk_t old, chunk_t new),
419 void *callback_context,
420 int *full)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000421{
422 int r;
423 unsigned int i;
424 struct disk_exception de;
425
426 /* presume the area is full */
427 *full = 1;
428
429 for (i = 0; i < ps->exceptions_per_area; i++) {
430 read_exception(ps, i, &de);
431
432 /*
433 * If the new_chunk is pointing at the start of
434 * the COW device, where the first metadata area
435 * is we know that we've hit the end of the
436 * exceptions. Therefore the area is not full.
437 */
438 if (de.new_chunk == 0LL) {
439 ps->current_committed = i;
440 *full = 0;
441 break;
442 }
443
444 /*
445 * Keep track of the start of the free chunks.
446 */
447 if (ps->next_free <= de.new_chunk)
448 ps->next_free = de.new_chunk + 1;
449
450 /*
451 * Otherwise we add the exception to the snapshot.
452 */
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000453 r = callback(callback_context, de.old_chunk, de.new_chunk);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000454 if (r)
455 return r;
456 }
457
458 return 0;
459}
460
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000461static int read_exceptions(struct pstore *ps,
462 int (*callback)(void *callback_context, chunk_t old,
463 chunk_t new),
464 void *callback_context)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000465{
466 int r, full = 1;
467
468 /*
469 * Keeping reading chunks and inserting exceptions until
470 * we find a partially full area.
471 */
472 for (ps->current_area = 0; full; ps->current_area++) {
473 r = area_io(ps, READ);
474 if (r)
475 return r;
476
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000477 r = insert_exceptions(ps, callback, callback_context, &full);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000478 if (r)
479 return r;
480 }
481
482 ps->current_area--;
483
484 return 0;
485}
486
487static struct pstore *get_info(struct dm_exception_store *store)
488{
489 return (struct pstore *) store->context;
490}
491
Mike Snitzer985903b2009-12-10 23:52:11 +0000492static void persistent_usage(struct dm_exception_store *store,
493 sector_t *total_sectors,
494 sector_t *sectors_allocated,
495 sector_t *metadata_sectors)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000496{
Mike Snitzer985903b2009-12-10 23:52:11 +0000497 struct pstore *ps = get_info(store);
498
499 *sectors_allocated = ps->next_free * store->chunk_size;
500 *total_sectors = get_dev_size(store->cow->bdev);
501
502 /*
503 * First chunk is the fixed header.
504 * Then there are (ps->current_area + 1) metadata chunks, each one
505 * separated from the next by ps->exceptions_per_area data chunks.
506 */
507 *metadata_sectors = (ps->current_area + 2) * store->chunk_size;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000508}
509
Jonathan Brassow493df712009-04-02 19:55:31 +0100510static void persistent_dtr(struct dm_exception_store *store)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000511{
512 struct pstore *ps = get_info(store);
513
514 destroy_workqueue(ps->metadata_wq);
Jonathan Brassowa32079c2009-04-02 19:55:35 +0100515
516 /* Created in read_header */
517 if (ps->io_client)
518 dm_io_client_destroy(ps->io_client);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000519 free_area(ps);
Jonathan Brassowa32079c2009-04-02 19:55:35 +0100520
521 /* Allocated in persistent_read_metadata */
522 if (ps->callbacks)
523 vfree(ps->callbacks);
524
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000525 kfree(ps);
526}
527
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000528static int persistent_read_metadata(struct dm_exception_store *store,
529 int (*callback)(void *callback_context,
530 chunk_t old, chunk_t new),
531 void *callback_context)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000532{
533 int r, uninitialized_var(new_snapshot);
534 struct pstore *ps = get_info(store);
535
536 /*
537 * Read the snapshot header.
538 */
539 r = read_header(ps, &new_snapshot);
540 if (r)
541 return r;
542
543 /*
544 * Now we know correct chunk_size, complete the initialisation.
545 */
Jonathan Brassow71fab002009-04-02 19:55:33 +0100546 ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
547 sizeof(struct disk_exception);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000548 ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
549 sizeof(*ps->callbacks));
550 if (!ps->callbacks)
551 return -ENOMEM;
552
553 /*
554 * Do we need to setup a new snapshot ?
555 */
556 if (new_snapshot) {
557 r = write_header(ps);
558 if (r) {
559 DMWARN("write_header failed");
560 return r;
561 }
562
563 ps->current_area = 0;
564 zero_memory_area(ps);
565 r = zero_disk_area(ps, 0);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000566 if (r)
Jon Brassowf5acc832009-12-10 23:52:07 +0000567 DMWARN("zero_disk_area(0) failed");
568 return r;
569 }
570 /*
571 * Sanity checks.
572 */
573 if (ps->version != SNAPSHOT_DISK_VERSION) {
574 DMWARN("unable to handle snapshot disk version %d",
575 ps->version);
576 return -EINVAL;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000577 }
578
Jon Brassowf5acc832009-12-10 23:52:07 +0000579 /*
580 * Metadata are valid, but snapshot is invalidated
581 */
582 if (!ps->valid)
583 return 1;
584
585 /*
586 * Read the metadata.
587 */
588 r = read_exceptions(ps, callback, callback_context);
589
590 return r;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000591}
592
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000593static int persistent_prepare_exception(struct dm_exception_store *store,
Jon Brassow1d4989c2009-12-10 23:52:10 +0000594 struct dm_exception *e)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000595{
596 struct pstore *ps = get_info(store);
597 uint32_t stride;
598 chunk_t next_free;
Jonathan Brassow49beb2b2009-04-02 19:55:33 +0100599 sector_t size = get_dev_size(store->cow->bdev);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000600
601 /* Is there enough room ? */
Jonathan Brassowd0216842009-04-02 19:55:32 +0100602 if (size < ((ps->next_free + 1) * store->chunk_size))
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000603 return -ENOSPC;
604
605 e->new_chunk = ps->next_free;
606
607 /*
608 * Move onto the next free pending, making sure to take
609 * into account the location of the metadata chunks.
610 */
611 stride = (ps->exceptions_per_area + 1);
612 next_free = ++ps->next_free;
613 if (sector_div(next_free, stride) == 1)
614 ps->next_free++;
615
616 atomic_inc(&ps->pending_count);
617 return 0;
618}
619
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000620static void persistent_commit_exception(struct dm_exception_store *store,
Jon Brassow1d4989c2009-12-10 23:52:10 +0000621 struct dm_exception *e,
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000622 void (*callback) (void *, int success),
623 void *callback_context)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000624{
625 unsigned int i;
626 struct pstore *ps = get_info(store);
627 struct disk_exception de;
628 struct commit_callback *cb;
629
630 de.old_chunk = e->old_chunk;
631 de.new_chunk = e->new_chunk;
632 write_exception(ps, ps->current_committed++, &de);
633
634 /*
635 * Add the callback to the back of the array. This code
636 * is the only place where the callback array is
637 * manipulated, and we know that it will never be called
638 * multiple times concurrently.
639 */
640 cb = ps->callbacks + ps->callback_count++;
641 cb->callback = callback;
642 cb->context = callback_context;
643
644 /*
645 * If there are exceptions in flight and we have not yet
646 * filled this metadata area there's nothing more to do.
647 */
648 if (!atomic_dec_and_test(&ps->pending_count) &&
649 (ps->current_committed != ps->exceptions_per_area))
650 return;
651
652 /*
653 * If we completely filled the current area, then wipe the next one.
654 */
655 if ((ps->current_committed == ps->exceptions_per_area) &&
656 zero_disk_area(ps, ps->current_area + 1))
657 ps->valid = 0;
658
659 /*
660 * Commit exceptions to disk.
661 */
Mikulas Patocka2bd02342009-06-22 10:12:26 +0100662 if (ps->valid && area_io(ps, WRITE_BARRIER))
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000663 ps->valid = 0;
664
665 /*
666 * Advance to the next area if this one is full.
667 */
668 if (ps->current_committed == ps->exceptions_per_area) {
669 ps->current_committed = 0;
670 ps->current_area++;
671 zero_memory_area(ps);
672 }
673
674 for (i = 0; i < ps->callback_count; i++) {
675 cb = ps->callbacks + i;
676 cb->callback(cb->context, ps->valid);
677 }
678
679 ps->callback_count = 0;
680}
681
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000682static void persistent_drop_snapshot(struct dm_exception_store *store)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000683{
684 struct pstore *ps = get_info(store);
685
686 ps->valid = 0;
687 if (write_header(ps))
688 DMWARN("write header failed");
689}
690
Jonathan Brassow493df712009-04-02 19:55:31 +0100691static int persistent_ctr(struct dm_exception_store *store,
692 unsigned argc, char **argv)
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000693{
694 struct pstore *ps;
695
696 /* allocate the pstore */
Jonathan Brassowa32079c2009-04-02 19:55:35 +0100697 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000698 if (!ps)
699 return -ENOMEM;
700
Jonathan Brassow71fab002009-04-02 19:55:33 +0100701 ps->store = store;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000702 ps->valid = 1;
703 ps->version = SNAPSHOT_DISK_VERSION;
704 ps->area = NULL;
Mikulas Patocka61578dc2009-09-04 20:40:39 +0100705 ps->zero_area = NULL;
706 ps->header_area = NULL;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000707 ps->next_free = 2; /* skipping the header and first area */
708 ps->current_committed = 0;
709
710 ps->callback_count = 0;
711 atomic_set(&ps->pending_count, 0);
712 ps->callbacks = NULL;
713
714 ps->metadata_wq = create_singlethread_workqueue("ksnaphd");
715 if (!ps->metadata_wq) {
716 kfree(ps);
717 DMERR("couldn't start header metadata update thread");
718 return -ENOMEM;
719 }
720
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000721 store->context = ps;
722
723 return 0;
724}
725
Jonathan Brassow1e302a92009-04-02 19:55:35 +0100726static unsigned persistent_status(struct dm_exception_store *store,
727 status_type_t status, char *result,
728 unsigned maxlen)
Jonathan Brassow493df712009-04-02 19:55:31 +0100729{
Jonathan Brassow1e302a92009-04-02 19:55:35 +0100730 unsigned sz = 0;
731
732 switch (status) {
733 case STATUSTYPE_INFO:
734 break;
735 case STATUSTYPE_TABLE:
736 DMEMIT(" %s P %llu", store->cow->name,
737 (unsigned long long)store->chunk_size);
738 }
Jonathan Brassow493df712009-04-02 19:55:31 +0100739
740 return sz;
741}
742
743static struct dm_exception_store_type _persistent_type = {
744 .name = "persistent",
745 .module = THIS_MODULE,
746 .ctr = persistent_ctr,
747 .dtr = persistent_dtr,
748 .read_metadata = persistent_read_metadata,
749 .prepare_exception = persistent_prepare_exception,
750 .commit_exception = persistent_commit_exception,
751 .drop_snapshot = persistent_drop_snapshot,
Mike Snitzer985903b2009-12-10 23:52:11 +0000752 .usage = persistent_usage,
Jonathan Brassow493df712009-04-02 19:55:31 +0100753 .status = persistent_status,
754};
755
756static struct dm_exception_store_type _persistent_compat_type = {
757 .name = "P",
758 .module = THIS_MODULE,
759 .ctr = persistent_ctr,
760 .dtr = persistent_dtr,
761 .read_metadata = persistent_read_metadata,
762 .prepare_exception = persistent_prepare_exception,
763 .commit_exception = persistent_commit_exception,
764 .drop_snapshot = persistent_drop_snapshot,
Mike Snitzer985903b2009-12-10 23:52:11 +0000765 .usage = persistent_usage,
Jonathan Brassow493df712009-04-02 19:55:31 +0100766 .status = persistent_status,
767};
768
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000769int dm_persistent_snapshot_init(void)
770{
Jonathan Brassow493df712009-04-02 19:55:31 +0100771 int r;
772
773 r = dm_exception_store_type_register(&_persistent_type);
774 if (r) {
775 DMERR("Unable to register persistent exception store type");
776 return r;
777 }
778
779 r = dm_exception_store_type_register(&_persistent_compat_type);
780 if (r) {
781 DMERR("Unable to register old-style persistent exception "
782 "store type");
783 dm_exception_store_type_unregister(&_persistent_type);
784 return r;
785 }
786
787 return r;
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000788}
789
790void dm_persistent_snapshot_exit(void)
791{
Jonathan Brassow493df712009-04-02 19:55:31 +0100792 dm_exception_store_type_unregister(&_persistent_type);
793 dm_exception_store_type_unregister(&_persistent_compat_type);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +0000794}