xref: /linux/drivers/md/dm-snap-persistent.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
4  * Copyright (C) 2006-2008 Red Hat GmbH
5  *
6  * This file is released under the GPL.
7  */
8 
9 #include "dm-exception-store.h"
10 
11 #include <linux/ctype.h>
12 #include <linux/mm.h>
13 #include <linux/pagemap.h>
14 #include <linux/vmalloc.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/dm-io.h>
18 #include <linux/dm-bufio.h>
19 
20 #define DM_MSG_PREFIX "persistent snapshot"
21 #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32U	/* 16KB */
22 
23 #define DM_PREFETCH_CHUNKS		12
24 
25 /*
26  *---------------------------------------------------------------
27  * Persistent snapshots, by persistent we mean that the snapshot
28  * will survive a reboot.
29  *---------------------------------------------------------------
30  */
31 
32 /*
33  * We need to store a record of which parts of the origin have
34  * been copied to the snapshot device.  The snapshot code
35  * requires that we copy exception chunks to chunk aligned areas
36  * of the COW store.  It makes sense therefore, to store the
37  * metadata in chunk size blocks.
38  *
39  * There is no backward or forward compatibility implemented,
40  * snapshots with different disk versions than the kernel will
41  * not be usable.  It is expected that "lvcreate" will blank out
42  * the start of a fresh COW device before calling the snapshot
43  * constructor.
44  *
45  * The first chunk of the COW device just contains the header.
46  * After this there is a chunk filled with exception metadata,
47  * followed by as many exception chunks as can fit in the
48  * metadata areas.
49  *
50  * All on disk structures are in little-endian format.  The end
51  * of the exceptions info is indicated by an exception with a
52  * new_chunk of 0, which is invalid since it would point to the
53  * header chunk.
54  */
55 
56 /*
57  * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
58  */
59 #define SNAP_MAGIC 0x70416e53
60 
61 /*
62  * The on-disk version of the metadata.
63  */
64 #define SNAPSHOT_DISK_VERSION 1
65 
66 #define NUM_SNAPSHOT_HDR_CHUNKS 1
67 
68 struct disk_header {
69 	__le32 magic;
70 
71 	/*
72 	 * Is this snapshot valid.  There is no way of recovering
73 	 * an invalid snapshot.
74 	 */
75 	__le32 valid;
76 
77 	/*
78 	 * Simple, incrementing version. no backward
79 	 * compatibility.
80 	 */
81 	__le32 version;
82 
83 	/* In sectors */
84 	__le32 chunk_size;
85 } __packed;
86 
87 struct disk_exception {
88 	__le64 old_chunk;
89 	__le64 new_chunk;
90 } __packed;
91 
92 struct core_exception {
93 	uint64_t old_chunk;
94 	uint64_t new_chunk;
95 };
96 
97 struct commit_callback {
98 	void (*callback)(void *ref, int success);
99 	void *context;
100 };
101 
102 /*
103  * The top level structure for a persistent exception store.
104  */
105 struct pstore {
106 	struct dm_exception_store *store;
107 	int version;
108 	int valid;
109 	uint32_t exceptions_per_area;
110 
111 	/*
112 	 * Now that we have an asynchronous kcopyd there is no
113 	 * need for large chunk sizes, so it wont hurt to have a
114 	 * whole chunks worth of metadata in memory at once.
115 	 */
116 	void *area;
117 
118 	/*
119 	 * An area of zeros used to clear the next area.
120 	 */
121 	void *zero_area;
122 
123 	/*
124 	 * An area used for header. The header can be written
125 	 * concurrently with metadata (when invalidating the snapshot),
126 	 * so it needs a separate buffer.
127 	 */
128 	void *header_area;
129 
130 	/*
131 	 * Used to keep track of which metadata area the data in
132 	 * 'chunk' refers to.
133 	 */
134 	chunk_t current_area;
135 
136 	/*
137 	 * The next free chunk for an exception.
138 	 *
139 	 * When creating exceptions, all the chunks here and above are
140 	 * free.  It holds the next chunk to be allocated.  On rare
141 	 * occasions (e.g. after a system crash) holes can be left in
142 	 * the exception store because chunks can be committed out of
143 	 * order.
144 	 *
145 	 * When merging exceptions, it does not necessarily mean all the
146 	 * chunks here and above are free.  It holds the value it would
147 	 * have held if all chunks had been committed in order of
148 	 * allocation.  Consequently the value may occasionally be
149 	 * slightly too low, but since it's only used for 'status' and
150 	 * it can never reach its minimum value too early this doesn't
151 	 * matter.
152 	 */
153 
154 	chunk_t next_free;
155 
156 	/*
157 	 * The index of next free exception in the current
158 	 * metadata area.
159 	 */
160 	uint32_t current_committed;
161 
162 	atomic_t pending_count;
163 	uint32_t callback_count;
164 	struct commit_callback *callbacks;
165 	struct dm_io_client *io_client;
166 
167 	struct workqueue_struct *metadata_wq;
168 };
169 
170 static int alloc_area(struct pstore *ps)
171 {
172 	int r = -ENOMEM;
173 	size_t len;
174 
175 	len = ps->store->chunk_size << SECTOR_SHIFT;
176 
177 	/*
178 	 * Allocate the chunk_size block of memory that will hold
179 	 * a single metadata area.
180 	 */
181 	ps->area = vmalloc(len);
182 	if (!ps->area)
183 		goto err_area;
184 
185 	ps->zero_area = vzalloc(len);
186 	if (!ps->zero_area)
187 		goto err_zero_area;
188 
189 	ps->header_area = vmalloc(len);
190 	if (!ps->header_area)
191 		goto err_header_area;
192 
193 	return 0;
194 
195 err_header_area:
196 	vfree(ps->zero_area);
197 
198 err_zero_area:
199 	vfree(ps->area);
200 
201 err_area:
202 	return r;
203 }
204 
205 static void free_area(struct pstore *ps)
206 {
207 	vfree(ps->area);
208 	ps->area = NULL;
209 	vfree(ps->zero_area);
210 	ps->zero_area = NULL;
211 	vfree(ps->header_area);
212 	ps->header_area = NULL;
213 }
214 
215 struct mdata_req {
216 	struct dm_io_region *where;
217 	struct dm_io_request *io_req;
218 	struct work_struct work;
219 	int result;
220 };
221 
222 static void do_metadata(struct work_struct *work)
223 {
224 	struct mdata_req *req = container_of(work, struct mdata_req, work);
225 
226 	req->result = dm_io(req->io_req, 1, req->where, NULL, NULL, IOPRIO_DEFAULT);
227 }
228 
229 /*
230  * Read or write a chunk aligned and sized block of data from a device.
231  */
232 static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, blk_opf_t opf,
233 		    int metadata)
234 {
235 	struct dm_io_region where = {
236 		.bdev = dm_snap_cow(ps->store->snap)->bdev,
237 		.sector = ps->store->chunk_size * chunk,
238 		.count = ps->store->chunk_size,
239 	};
240 	struct dm_io_request io_req = {
241 		.bi_opf = opf,
242 		.mem.type = DM_IO_VMA,
243 		.mem.ptr.vma = area,
244 		.client = ps->io_client,
245 		.notify.fn = NULL,
246 	};
247 	struct mdata_req req;
248 
249 	if (!metadata)
250 		return dm_io(&io_req, 1, &where, NULL, NULL, IOPRIO_DEFAULT);
251 
252 	req.where = &where;
253 	req.io_req = &io_req;
254 
255 	/*
256 	 * Issue the synchronous I/O from a different thread
257 	 * to avoid submit_bio_noacct recursion.
258 	 */
259 	INIT_WORK_ONSTACK(&req.work, do_metadata);
260 	queue_work(ps->metadata_wq, &req.work);
261 	flush_workqueue(ps->metadata_wq);
262 	destroy_work_on_stack(&req.work);
263 
264 	return req.result;
265 }
266 
267 /*
268  * Convert a metadata area index to a chunk index.
269  */
270 static chunk_t area_location(struct pstore *ps, chunk_t area)
271 {
272 	return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area);
273 }
274 
275 static void skip_metadata(struct pstore *ps)
276 {
277 	uint32_t stride = ps->exceptions_per_area + 1;
278 	chunk_t next_free = ps->next_free;
279 
280 	if (sector_div(next_free, stride) == NUM_SNAPSHOT_HDR_CHUNKS)
281 		ps->next_free++;
282 }
283 
284 /*
285  * Read or write a metadata area.  Remembering to skip the first
286  * chunk which holds the header.
287  */
288 static int area_io(struct pstore *ps, blk_opf_t opf)
289 {
290 	chunk_t chunk = area_location(ps, ps->current_area);
291 
292 	return chunk_io(ps, ps->area, chunk, opf, 0);
293 }
294 
295 static void zero_memory_area(struct pstore *ps)
296 {
297 	memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
298 }
299 
300 static int zero_disk_area(struct pstore *ps, chunk_t area)
301 {
302 	return chunk_io(ps, ps->zero_area, area_location(ps, area),
303 			REQ_OP_WRITE, 0);
304 }
305 
306 static int read_header(struct pstore *ps, int *new_snapshot)
307 {
308 	int r;
309 	struct disk_header *dh;
310 	unsigned int chunk_size;
311 	int chunk_size_supplied = 1;
312 	char *chunk_err;
313 
314 	/*
315 	 * Use default chunk size (or logical_block_size, if larger)
316 	 * if none supplied
317 	 */
318 	if (!ps->store->chunk_size) {
319 		ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
320 		    bdev_logical_block_size(dm_snap_cow(ps->store->snap)->
321 					    bdev) >> 9);
322 		ps->store->chunk_mask = ps->store->chunk_size - 1;
323 		ps->store->chunk_shift = __ffs(ps->store->chunk_size);
324 		chunk_size_supplied = 0;
325 	}
326 
327 	ps->io_client = dm_io_client_create();
328 	if (IS_ERR(ps->io_client)) {
329 		r = PTR_ERR(ps->io_client);
330 		ps->io_client = NULL;
331 		return r;
332 	}
333 
334 	r = alloc_area(ps);
335 	if (r)
336 		return r;
337 
338 	r = chunk_io(ps, ps->header_area, 0, REQ_OP_READ, 1);
339 	if (r)
340 		goto bad;
341 
342 	dh = ps->header_area;
343 
344 	if (le32_to_cpu(dh->magic) == 0) {
345 		*new_snapshot = 1;
346 		return 0;
347 	}
348 
349 	if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
350 		DMWARN("Invalid or corrupt snapshot");
351 		r = -ENXIO;
352 		goto bad;
353 	}
354 
355 	*new_snapshot = 0;
356 	ps->valid = le32_to_cpu(dh->valid);
357 	ps->version = le32_to_cpu(dh->version);
358 	chunk_size = le32_to_cpu(dh->chunk_size);
359 
360 	if (ps->store->chunk_size == chunk_size)
361 		return 0;
362 
363 	if (chunk_size_supplied)
364 		DMWARN("chunk size %u in device metadata overrides table chunk size of %u.",
365 		       chunk_size, ps->store->chunk_size);
366 
367 	/* We had a bogus chunk_size. Fix stuff up. */
368 	free_area(ps);
369 
370 	r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
371 					      &chunk_err);
372 	if (r) {
373 		DMERR("invalid on-disk chunk size %u: %s.",
374 		      chunk_size, chunk_err);
375 		return r;
376 	}
377 
378 	r = alloc_area(ps);
379 	return r;
380 
381 bad:
382 	free_area(ps);
383 	return r;
384 }
385 
386 static int write_header(struct pstore *ps)
387 {
388 	struct disk_header *dh;
389 
390 	memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
391 
392 	dh = ps->header_area;
393 	dh->magic = cpu_to_le32(SNAP_MAGIC);
394 	dh->valid = cpu_to_le32(ps->valid);
395 	dh->version = cpu_to_le32(ps->version);
396 	dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
397 
398 	return chunk_io(ps, ps->header_area, 0, REQ_OP_WRITE, 1);
399 }
400 
401 /*
402  * Access functions for the disk exceptions, these do the endian conversions.
403  */
404 static struct disk_exception *get_exception(struct pstore *ps, void *ps_area,
405 					    uint32_t index)
406 {
407 	BUG_ON(index >= ps->exceptions_per_area);
408 
409 	return ((struct disk_exception *) ps_area) + index;
410 }
411 
412 static void read_exception(struct pstore *ps, void *ps_area,
413 			   uint32_t index, struct core_exception *result)
414 {
415 	struct disk_exception *de = get_exception(ps, ps_area, index);
416 
417 	/* copy it */
418 	result->old_chunk = le64_to_cpu(de->old_chunk);
419 	result->new_chunk = le64_to_cpu(de->new_chunk);
420 }
421 
422 static void write_exception(struct pstore *ps,
423 			    uint32_t index, struct core_exception *e)
424 {
425 	struct disk_exception *de = get_exception(ps, ps->area, index);
426 
427 	/* copy it */
428 	de->old_chunk = cpu_to_le64(e->old_chunk);
429 	de->new_chunk = cpu_to_le64(e->new_chunk);
430 }
431 
432 static void clear_exception(struct pstore *ps, uint32_t index)
433 {
434 	struct disk_exception *de = get_exception(ps, ps->area, index);
435 
436 	/* clear it */
437 	de->old_chunk = 0;
438 	de->new_chunk = 0;
439 }
440 
441 /*
442  * Registers the exceptions that are present in the current area.
443  * 'full' is filled in to indicate if the area has been
444  * filled.
445  */
446 static int insert_exceptions(struct pstore *ps, void *ps_area,
447 			     int (*callback)(void *callback_context,
448 					     chunk_t old, chunk_t new),
449 			     void *callback_context,
450 			     int *full)
451 {
452 	int r;
453 	unsigned int i;
454 	struct core_exception e;
455 
456 	/* presume the area is full */
457 	*full = 1;
458 
459 	for (i = 0; i < ps->exceptions_per_area; i++) {
460 		read_exception(ps, ps_area, i, &e);
461 
462 		/*
463 		 * If the new_chunk is pointing at the start of
464 		 * the COW device, where the first metadata area
465 		 * is we know that we've hit the end of the
466 		 * exceptions.  Therefore the area is not full.
467 		 */
468 		if (e.new_chunk == 0LL) {
469 			ps->current_committed = i;
470 			*full = 0;
471 			break;
472 		}
473 
474 		/*
475 		 * Keep track of the start of the free chunks.
476 		 */
477 		if (ps->next_free <= e.new_chunk)
478 			ps->next_free = e.new_chunk + 1;
479 
480 		/*
481 		 * Otherwise we add the exception to the snapshot.
482 		 */
483 		r = callback(callback_context, e.old_chunk, e.new_chunk);
484 		if (r)
485 			return r;
486 	}
487 
488 	return 0;
489 }
490 
491 static int read_exceptions(struct pstore *ps,
492 			   int (*callback)(void *callback_context, chunk_t old,
493 					   chunk_t new),
494 			   void *callback_context)
495 {
496 	int r, full = 1;
497 	struct dm_bufio_client *client;
498 	chunk_t prefetch_area = 0;
499 
500 	client = dm_bufio_client_create(dm_snap_cow(ps->store->snap)->bdev,
501 					ps->store->chunk_size << SECTOR_SHIFT,
502 					1, 0, NULL, NULL, 0);
503 
504 	if (IS_ERR(client))
505 		return PTR_ERR(client);
506 
507 	/*
508 	 * Setup for one current buffer + desired readahead buffers.
509 	 */
510 	dm_bufio_set_minimum_buffers(client, 1 + DM_PREFETCH_CHUNKS);
511 
512 	/*
513 	 * Keeping reading chunks and inserting exceptions until
514 	 * we find a partially full area.
515 	 */
516 	for (ps->current_area = 0; full; ps->current_area++) {
517 		struct dm_buffer *bp;
518 		void *area;
519 		chunk_t chunk;
520 
521 		if (unlikely(prefetch_area < ps->current_area))
522 			prefetch_area = ps->current_area;
523 
524 		if (DM_PREFETCH_CHUNKS) {
525 			do {
526 				chunk_t pf_chunk = area_location(ps, prefetch_area);
527 
528 				if (unlikely(pf_chunk >= dm_bufio_get_device_size(client)))
529 					break;
530 				dm_bufio_prefetch(client, pf_chunk, 1);
531 				prefetch_area++;
532 				if (unlikely(!prefetch_area))
533 					break;
534 			} while (prefetch_area <= ps->current_area + DM_PREFETCH_CHUNKS);
535 		}
536 
537 		chunk = area_location(ps, ps->current_area);
538 
539 		area = dm_bufio_read(client, chunk, &bp);
540 		if (IS_ERR(area)) {
541 			r = PTR_ERR(area);
542 			goto ret_destroy_bufio;
543 		}
544 
545 		r = insert_exceptions(ps, area, callback, callback_context,
546 				      &full);
547 
548 		if (!full)
549 			memcpy(ps->area, area, ps->store->chunk_size << SECTOR_SHIFT);
550 
551 		dm_bufio_release(bp);
552 
553 		dm_bufio_forget(client, chunk);
554 
555 		if (unlikely(r))
556 			goto ret_destroy_bufio;
557 	}
558 
559 	ps->current_area--;
560 
561 	skip_metadata(ps);
562 
563 	r = 0;
564 
565 ret_destroy_bufio:
566 	dm_bufio_client_destroy(client);
567 
568 	return r;
569 }
570 
571 static struct pstore *get_info(struct dm_exception_store *store)
572 {
573 	return store->context;
574 }
575 
576 static void persistent_usage(struct dm_exception_store *store,
577 			     sector_t *total_sectors,
578 			     sector_t *sectors_allocated,
579 			     sector_t *metadata_sectors)
580 {
581 	struct pstore *ps = get_info(store);
582 
583 	*sectors_allocated = ps->next_free * store->chunk_size;
584 	*total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
585 
586 	/*
587 	 * First chunk is the fixed header.
588 	 * Then there are (ps->current_area + 1) metadata chunks, each one
589 	 * separated from the next by ps->exceptions_per_area data chunks.
590 	 */
591 	*metadata_sectors = (ps->current_area + 1 + NUM_SNAPSHOT_HDR_CHUNKS) *
592 			    store->chunk_size;
593 }
594 
595 static void persistent_dtr(struct dm_exception_store *store)
596 {
597 	struct pstore *ps = get_info(store);
598 
599 	destroy_workqueue(ps->metadata_wq);
600 
601 	/* Created in read_header */
602 	if (ps->io_client)
603 		dm_io_client_destroy(ps->io_client);
604 	free_area(ps);
605 
606 	/* Allocated in persistent_read_metadata */
607 	kvfree(ps->callbacks);
608 
609 	kfree(ps);
610 }
611 
612 static int persistent_read_metadata(struct dm_exception_store *store,
613 				    int (*callback)(void *callback_context,
614 						    chunk_t old, chunk_t new),
615 				    void *callback_context)
616 {
617 	int r, new_snapshot;
618 	struct pstore *ps = get_info(store);
619 
620 	/*
621 	 * Read the snapshot header.
622 	 */
623 	r = read_header(ps, &new_snapshot);
624 	if (r)
625 		return r;
626 
627 	/*
628 	 * Now we know correct chunk_size, complete the initialisation.
629 	 */
630 	ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
631 				  sizeof(struct disk_exception);
632 	ps->callbacks = kvzalloc_objs(*ps->callbacks, ps->exceptions_per_area);
633 	if (!ps->callbacks)
634 		return -ENOMEM;
635 
636 	/*
637 	 * Do we need to setup a new snapshot ?
638 	 */
639 	if (new_snapshot) {
640 		r = write_header(ps);
641 		if (r) {
642 			DMWARN("write_header failed");
643 			return r;
644 		}
645 
646 		ps->current_area = 0;
647 		zero_memory_area(ps);
648 		r = zero_disk_area(ps, 0);
649 		if (r)
650 			DMWARN("zero_disk_area(0) failed");
651 		return r;
652 	}
653 	/*
654 	 * Sanity checks.
655 	 */
656 	if (ps->version != SNAPSHOT_DISK_VERSION) {
657 		DMWARN("unable to handle snapshot disk version %d",
658 		       ps->version);
659 		return -EINVAL;
660 	}
661 
662 	/*
663 	 * Metadata are valid, but snapshot is invalidated
664 	 */
665 	if (!ps->valid)
666 		return 1;
667 
668 	/*
669 	 * Read the metadata.
670 	 */
671 	r = read_exceptions(ps, callback, callback_context);
672 
673 	return r;
674 }
675 
676 static int persistent_prepare_exception(struct dm_exception_store *store,
677 					struct dm_exception *e)
678 {
679 	struct pstore *ps = get_info(store);
680 	sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
681 
682 	/* Is there enough room ? */
683 	if (size < ((ps->next_free + 1) * store->chunk_size))
684 		return -ENOSPC;
685 
686 	e->new_chunk = ps->next_free;
687 
688 	/*
689 	 * Move onto the next free pending, making sure to take
690 	 * into account the location of the metadata chunks.
691 	 */
692 	ps->next_free++;
693 	skip_metadata(ps);
694 
695 	atomic_inc(&ps->pending_count);
696 	return 0;
697 }
698 
699 static void persistent_commit_exception(struct dm_exception_store *store,
700 					struct dm_exception *e, int valid,
701 					void (*callback)(void *, int success),
702 					void *callback_context)
703 {
704 	unsigned int i;
705 	struct pstore *ps = get_info(store);
706 	struct core_exception ce;
707 	struct commit_callback *cb;
708 
709 	if (!valid)
710 		ps->valid = 0;
711 
712 	ce.old_chunk = e->old_chunk;
713 	ce.new_chunk = e->new_chunk;
714 	write_exception(ps, ps->current_committed++, &ce);
715 
716 	/*
717 	 * Add the callback to the back of the array.  This code
718 	 * is the only place where the callback array is
719 	 * manipulated, and we know that it will never be called
720 	 * multiple times concurrently.
721 	 */
722 	cb = ps->callbacks + ps->callback_count++;
723 	cb->callback = callback;
724 	cb->context = callback_context;
725 
726 	/*
727 	 * If there are exceptions in flight and we have not yet
728 	 * filled this metadata area there's nothing more to do.
729 	 */
730 	if (!atomic_dec_and_test(&ps->pending_count) &&
731 	    (ps->current_committed != ps->exceptions_per_area))
732 		return;
733 
734 	/*
735 	 * If we completely filled the current area, then wipe the next one.
736 	 */
737 	if ((ps->current_committed == ps->exceptions_per_area) &&
738 	    zero_disk_area(ps, ps->current_area + 1))
739 		ps->valid = 0;
740 
741 	/*
742 	 * Commit exceptions to disk.
743 	 */
744 	if (ps->valid && area_io(ps, REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA |
745 				 REQ_SYNC))
746 		ps->valid = 0;
747 
748 	/*
749 	 * Advance to the next area if this one is full.
750 	 */
751 	if (ps->current_committed == ps->exceptions_per_area) {
752 		ps->current_committed = 0;
753 		ps->current_area++;
754 		zero_memory_area(ps);
755 	}
756 
757 	for (i = 0; i < ps->callback_count; i++) {
758 		cb = ps->callbacks + i;
759 		cb->callback(cb->context, ps->valid);
760 	}
761 
762 	ps->callback_count = 0;
763 }
764 
765 static int persistent_prepare_merge(struct dm_exception_store *store,
766 				    chunk_t *last_old_chunk,
767 				    chunk_t *last_new_chunk)
768 {
769 	struct pstore *ps = get_info(store);
770 	struct core_exception ce;
771 	int nr_consecutive;
772 	int r;
773 
774 	/*
775 	 * When current area is empty, move back to preceding area.
776 	 */
777 	if (!ps->current_committed) {
778 		/*
779 		 * Have we finished?
780 		 */
781 		if (!ps->current_area)
782 			return 0;
783 
784 		ps->current_area--;
785 		r = area_io(ps, REQ_OP_READ);
786 		if (r < 0)
787 			return r;
788 		ps->current_committed = ps->exceptions_per_area;
789 	}
790 
791 	read_exception(ps, ps->area, ps->current_committed - 1, &ce);
792 	*last_old_chunk = ce.old_chunk;
793 	*last_new_chunk = ce.new_chunk;
794 
795 	/*
796 	 * Find number of consecutive chunks within the current area,
797 	 * working backwards.
798 	 */
799 	for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
800 	     nr_consecutive++) {
801 		read_exception(ps, ps->area,
802 			       ps->current_committed - 1 - nr_consecutive, &ce);
803 		if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
804 		    ce.new_chunk != *last_new_chunk - nr_consecutive)
805 			break;
806 	}
807 
808 	return nr_consecutive;
809 }
810 
811 static int persistent_commit_merge(struct dm_exception_store *store,
812 				   int nr_merged)
813 {
814 	int r, i;
815 	struct pstore *ps = get_info(store);
816 
817 	BUG_ON(nr_merged > ps->current_committed);
818 
819 	for (i = 0; i < nr_merged; i++)
820 		clear_exception(ps, ps->current_committed - 1 - i);
821 
822 	r = area_io(ps, REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA);
823 	if (r < 0)
824 		return r;
825 
826 	ps->current_committed -= nr_merged;
827 
828 	/*
829 	 * At this stage, only persistent_usage() uses ps->next_free, so
830 	 * we make no attempt to keep ps->next_free strictly accurate
831 	 * as exceptions may have been committed out-of-order originally.
832 	 * Once a snapshot has become merging, we set it to the value it
833 	 * would have held had all the exceptions been committed in order.
834 	 *
835 	 * ps->current_area does not get reduced by prepare_merge() until
836 	 * after commit_merge() has removed the nr_merged previous exceptions.
837 	 */
838 	ps->next_free = area_location(ps, ps->current_area) +
839 			ps->current_committed + 1;
840 
841 	return 0;
842 }
843 
844 static void persistent_drop_snapshot(struct dm_exception_store *store)
845 {
846 	struct pstore *ps = get_info(store);
847 
848 	ps->valid = 0;
849 	if (write_header(ps))
850 		DMWARN("write header failed");
851 }
852 
853 static int persistent_ctr(struct dm_exception_store *store, char *options)
854 {
855 	struct pstore *ps;
856 	int r;
857 
858 	/* allocate the pstore */
859 	ps = kzalloc_obj(*ps);
860 	if (!ps)
861 		return -ENOMEM;
862 
863 	ps->store = store;
864 	ps->valid = 1;
865 	ps->version = SNAPSHOT_DISK_VERSION;
866 	ps->area = NULL;
867 	ps->zero_area = NULL;
868 	ps->header_area = NULL;
869 	ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
870 	ps->current_committed = 0;
871 
872 	ps->callback_count = 0;
873 	atomic_set(&ps->pending_count, 0);
874 	ps->callbacks = NULL;
875 
876 	ps->metadata_wq = alloc_workqueue("ksnaphd",
877 					  WQ_MEM_RECLAIM | WQ_PERCPU, 0);
878 	if (!ps->metadata_wq) {
879 		DMERR("couldn't start header metadata update thread");
880 		r = -ENOMEM;
881 		goto err_workqueue;
882 	}
883 
884 	if (options) {
885 		char overflow = toupper(options[0]);
886 
887 		if (overflow == 'O')
888 			store->userspace_supports_overflow = true;
889 		else {
890 			DMERR("Unsupported persistent store option: %s", options);
891 			r = -EINVAL;
892 			goto err_options;
893 		}
894 	}
895 
896 	store->context = ps;
897 
898 	return 0;
899 
900 err_options:
901 	destroy_workqueue(ps->metadata_wq);
902 err_workqueue:
903 	kfree(ps);
904 
905 	return r;
906 }
907 
908 static unsigned int persistent_status(struct dm_exception_store *store,
909 				  status_type_t status, char *result,
910 				  unsigned int maxlen)
911 {
912 	unsigned int sz = 0;
913 
914 	switch (status) {
915 	case STATUSTYPE_INFO:
916 		break;
917 	case STATUSTYPE_TABLE:
918 		DMEMIT(" %s %llu", store->userspace_supports_overflow ? "PO" : "P",
919 		       (unsigned long long)store->chunk_size);
920 		break;
921 	case STATUSTYPE_IMA:
922 		*result = '\0';
923 		break;
924 	}
925 
926 	return sz;
927 }
928 
929 static struct dm_exception_store_type _persistent_type = {
930 	.name = "persistent",
931 	.module = THIS_MODULE,
932 	.ctr = persistent_ctr,
933 	.dtr = persistent_dtr,
934 	.read_metadata = persistent_read_metadata,
935 	.prepare_exception = persistent_prepare_exception,
936 	.commit_exception = persistent_commit_exception,
937 	.prepare_merge = persistent_prepare_merge,
938 	.commit_merge = persistent_commit_merge,
939 	.drop_snapshot = persistent_drop_snapshot,
940 	.usage = persistent_usage,
941 	.status = persistent_status,
942 };
943 
944 static struct dm_exception_store_type _persistent_compat_type = {
945 	.name = "P",
946 	.module = THIS_MODULE,
947 	.ctr = persistent_ctr,
948 	.dtr = persistent_dtr,
949 	.read_metadata = persistent_read_metadata,
950 	.prepare_exception = persistent_prepare_exception,
951 	.commit_exception = persistent_commit_exception,
952 	.prepare_merge = persistent_prepare_merge,
953 	.commit_merge = persistent_commit_merge,
954 	.drop_snapshot = persistent_drop_snapshot,
955 	.usage = persistent_usage,
956 	.status = persistent_status,
957 };
958 
959 int dm_persistent_snapshot_init(void)
960 {
961 	int r;
962 
963 	r = dm_exception_store_type_register(&_persistent_type);
964 	if (r) {
965 		DMERR("Unable to register persistent exception store type");
966 		return r;
967 	}
968 
969 	r = dm_exception_store_type_register(&_persistent_compat_type);
970 	if (r) {
971 		DMERR("Unable to register old-style persistent exception store type");
972 		dm_exception_store_type_unregister(&_persistent_type);
973 		return r;
974 	}
975 
976 	return r;
977 }
978 
979 void dm_persistent_snapshot_exit(void)
980 {
981 	dm_exception_store_type_unregister(&_persistent_type);
982 	dm_exception_store_type_unregister(&_persistent_compat_type);
983 }
984