xref: /openbmc/linux/drivers/md/dm-snap-persistent.c (revision 55b082e614e219fb5199a6f93e648ed35d3c96d5)
14db6bfe0SAlasdair G Kergon /*
24db6bfe0SAlasdair G Kergon  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
34db6bfe0SAlasdair G Kergon  * Copyright (C) 2006-2008 Red Hat GmbH
44db6bfe0SAlasdair G Kergon  *
54db6bfe0SAlasdair G Kergon  * This file is released under the GPL.
64db6bfe0SAlasdair G Kergon  */
74db6bfe0SAlasdair G Kergon 
84db6bfe0SAlasdair G Kergon #include "dm-exception-store.h"
94db6bfe0SAlasdair G Kergon 
104db6bfe0SAlasdair G Kergon #include <linux/mm.h>
114db6bfe0SAlasdair G Kergon #include <linux/pagemap.h>
124db6bfe0SAlasdair G Kergon #include <linux/vmalloc.h>
13daaa5f7cSPaul Gortmaker #include <linux/export.h>
144db6bfe0SAlasdair G Kergon #include <linux/slab.h>
154db6bfe0SAlasdair G Kergon #include <linux/dm-io.h>
1655494bf2SMikulas Patocka #include "dm-bufio.h"
174db6bfe0SAlasdair G Kergon 
184db6bfe0SAlasdair G Kergon #define DM_MSG_PREFIX "persistent snapshot"
194db6bfe0SAlasdair G Kergon #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32	/* 16KB */
204db6bfe0SAlasdair G Kergon 
21*55b082e6SMikulas Patocka #define DM_PREFETCH_CHUNKS		12
22*55b082e6SMikulas Patocka 
234db6bfe0SAlasdair G Kergon /*-----------------------------------------------------------------
244db6bfe0SAlasdair G Kergon  * Persistent snapshots, by persistent we mean that the snapshot
254db6bfe0SAlasdair G Kergon  * will survive a reboot.
264db6bfe0SAlasdair G Kergon  *---------------------------------------------------------------*/
274db6bfe0SAlasdair G Kergon 
284db6bfe0SAlasdair G Kergon /*
294db6bfe0SAlasdair G Kergon  * We need to store a record of which parts of the origin have
304db6bfe0SAlasdair G Kergon  * been copied to the snapshot device.  The snapshot code
314db6bfe0SAlasdair G Kergon  * requires that we copy exception chunks to chunk aligned areas
324db6bfe0SAlasdair G Kergon  * of the COW store.  It makes sense therefore, to store the
334db6bfe0SAlasdair G Kergon  * metadata in chunk size blocks.
344db6bfe0SAlasdair G Kergon  *
354db6bfe0SAlasdair G Kergon  * There is no backward or forward compatibility implemented,
364db6bfe0SAlasdair G Kergon  * snapshots with different disk versions than the kernel will
374db6bfe0SAlasdair G Kergon  * not be usable.  It is expected that "lvcreate" will blank out
384db6bfe0SAlasdair G Kergon  * the start of a fresh COW device before calling the snapshot
394db6bfe0SAlasdair G Kergon  * constructor.
404db6bfe0SAlasdair G Kergon  *
414db6bfe0SAlasdair G Kergon  * The first chunk of the COW device just contains the header.
424db6bfe0SAlasdair G Kergon  * After this there is a chunk filled with exception metadata,
434db6bfe0SAlasdair G Kergon  * followed by as many exception chunks as can fit in the
444db6bfe0SAlasdair G Kergon  * metadata areas.
454db6bfe0SAlasdair G Kergon  *
464db6bfe0SAlasdair G Kergon  * All on disk structures are in little-endian format.  The end
474db6bfe0SAlasdair G Kergon  * of the exceptions info is indicated by an exception with a
484db6bfe0SAlasdair G Kergon  * new_chunk of 0, which is invalid since it would point to the
494db6bfe0SAlasdair G Kergon  * header chunk.
504db6bfe0SAlasdair G Kergon  */
514db6bfe0SAlasdair G Kergon 
524db6bfe0SAlasdair G Kergon /*
534db6bfe0SAlasdair G Kergon  * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
544db6bfe0SAlasdair G Kergon  */
554db6bfe0SAlasdair G Kergon #define SNAP_MAGIC 0x70416e53
564db6bfe0SAlasdair G Kergon 
574db6bfe0SAlasdair G Kergon /*
584db6bfe0SAlasdair G Kergon  * The on-disk version of the metadata.
594db6bfe0SAlasdair G Kergon  */
604db6bfe0SAlasdair G Kergon #define SNAPSHOT_DISK_VERSION 1
614db6bfe0SAlasdair G Kergon 
624454a621SMikulas Patocka #define NUM_SNAPSHOT_HDR_CHUNKS 1
634454a621SMikulas Patocka 
644db6bfe0SAlasdair G Kergon struct disk_header {
65283a8328SAlasdair G Kergon 	__le32 magic;
664db6bfe0SAlasdair G Kergon 
674db6bfe0SAlasdair G Kergon 	/*
684db6bfe0SAlasdair G Kergon 	 * Is this snapshot valid.  There is no way of recovering
694db6bfe0SAlasdair G Kergon 	 * an invalid snapshot.
704db6bfe0SAlasdair G Kergon 	 */
71283a8328SAlasdair G Kergon 	__le32 valid;
724db6bfe0SAlasdair G Kergon 
734db6bfe0SAlasdair G Kergon 	/*
744db6bfe0SAlasdair G Kergon 	 * Simple, incrementing version. no backward
754db6bfe0SAlasdair G Kergon 	 * compatibility.
764db6bfe0SAlasdair G Kergon 	 */
77283a8328SAlasdair G Kergon 	__le32 version;
784db6bfe0SAlasdair G Kergon 
794db6bfe0SAlasdair G Kergon 	/* In sectors */
80283a8328SAlasdair G Kergon 	__le32 chunk_size;
81283a8328SAlasdair G Kergon } __packed;
824db6bfe0SAlasdair G Kergon 
834db6bfe0SAlasdair G Kergon struct disk_exception {
84283a8328SAlasdair G Kergon 	__le64 old_chunk;
85283a8328SAlasdair G Kergon 	__le64 new_chunk;
86283a8328SAlasdair G Kergon } __packed;
87283a8328SAlasdair G Kergon 
88283a8328SAlasdair G Kergon struct core_exception {
894db6bfe0SAlasdair G Kergon 	uint64_t old_chunk;
904db6bfe0SAlasdair G Kergon 	uint64_t new_chunk;
914db6bfe0SAlasdair G Kergon };
924db6bfe0SAlasdair G Kergon 
934db6bfe0SAlasdair G Kergon struct commit_callback {
944db6bfe0SAlasdair G Kergon 	void (*callback)(void *, int success);
954db6bfe0SAlasdair G Kergon 	void *context;
964db6bfe0SAlasdair G Kergon };
974db6bfe0SAlasdair G Kergon 
984db6bfe0SAlasdair G Kergon /*
994db6bfe0SAlasdair G Kergon  * The top level structure for a persistent exception store.
1004db6bfe0SAlasdair G Kergon  */
1014db6bfe0SAlasdair G Kergon struct pstore {
10271fab00aSJonathan Brassow 	struct dm_exception_store *store;
1034db6bfe0SAlasdair G Kergon 	int version;
1044db6bfe0SAlasdair G Kergon 	int valid;
1054db6bfe0SAlasdair G Kergon 	uint32_t exceptions_per_area;
1064db6bfe0SAlasdair G Kergon 
1074db6bfe0SAlasdair G Kergon 	/*
1084db6bfe0SAlasdair G Kergon 	 * Now that we have an asynchronous kcopyd there is no
1094db6bfe0SAlasdair G Kergon 	 * need for large chunk sizes, so it wont hurt to have a
1104db6bfe0SAlasdair G Kergon 	 * whole chunks worth of metadata in memory at once.
1114db6bfe0SAlasdair G Kergon 	 */
1124db6bfe0SAlasdair G Kergon 	void *area;
1134db6bfe0SAlasdair G Kergon 
1144db6bfe0SAlasdair G Kergon 	/*
1154db6bfe0SAlasdair G Kergon 	 * An area of zeros used to clear the next area.
1164db6bfe0SAlasdair G Kergon 	 */
1174db6bfe0SAlasdair G Kergon 	void *zero_area;
1184db6bfe0SAlasdair G Kergon 
1194db6bfe0SAlasdair G Kergon 	/*
12061578dcdSMikulas Patocka 	 * An area used for header. The header can be written
12161578dcdSMikulas Patocka 	 * concurrently with metadata (when invalidating the snapshot),
12261578dcdSMikulas Patocka 	 * so it needs a separate buffer.
12361578dcdSMikulas Patocka 	 */
12461578dcdSMikulas Patocka 	void *header_area;
12561578dcdSMikulas Patocka 
12661578dcdSMikulas Patocka 	/*
1274db6bfe0SAlasdair G Kergon 	 * Used to keep track of which metadata area the data in
1284db6bfe0SAlasdair G Kergon 	 * 'chunk' refers to.
1294db6bfe0SAlasdair G Kergon 	 */
1304db6bfe0SAlasdair G Kergon 	chunk_t current_area;
1314db6bfe0SAlasdair G Kergon 
1324db6bfe0SAlasdair G Kergon 	/*
1334db6bfe0SAlasdair G Kergon 	 * The next free chunk for an exception.
1344454a621SMikulas Patocka 	 *
1354454a621SMikulas Patocka 	 * When creating exceptions, all the chunks here and above are
1364454a621SMikulas Patocka 	 * free.  It holds the next chunk to be allocated.  On rare
1374454a621SMikulas Patocka 	 * occasions (e.g. after a system crash) holes can be left in
1384454a621SMikulas Patocka 	 * the exception store because chunks can be committed out of
1394454a621SMikulas Patocka 	 * order.
1404454a621SMikulas Patocka 	 *
1414454a621SMikulas Patocka 	 * When merging exceptions, it does not necessarily mean all the
1424454a621SMikulas Patocka 	 * chunks here and above are free.  It holds the value it would
1434454a621SMikulas Patocka 	 * have held if all chunks had been committed in order of
1444454a621SMikulas Patocka 	 * allocation.  Consequently the value may occasionally be
1454454a621SMikulas Patocka 	 * slightly too low, but since it's only used for 'status' and
1464454a621SMikulas Patocka 	 * it can never reach its minimum value too early this doesn't
1474454a621SMikulas Patocka 	 * matter.
1484db6bfe0SAlasdair G Kergon 	 */
1494454a621SMikulas Patocka 
1504db6bfe0SAlasdair G Kergon 	chunk_t next_free;
1514db6bfe0SAlasdair G Kergon 
1524db6bfe0SAlasdair G Kergon 	/*
1534db6bfe0SAlasdair G Kergon 	 * The index of next free exception in the current
1544db6bfe0SAlasdair G Kergon 	 * metadata area.
1554db6bfe0SAlasdair G Kergon 	 */
1564db6bfe0SAlasdair G Kergon 	uint32_t current_committed;
1574db6bfe0SAlasdair G Kergon 
1584db6bfe0SAlasdair G Kergon 	atomic_t pending_count;
1594db6bfe0SAlasdair G Kergon 	uint32_t callback_count;
1604db6bfe0SAlasdair G Kergon 	struct commit_callback *callbacks;
1614db6bfe0SAlasdair G Kergon 	struct dm_io_client *io_client;
1624db6bfe0SAlasdair G Kergon 
1634db6bfe0SAlasdair G Kergon 	struct workqueue_struct *metadata_wq;
1644db6bfe0SAlasdair G Kergon };
1654db6bfe0SAlasdair G Kergon 
1664db6bfe0SAlasdair G Kergon static int alloc_area(struct pstore *ps)
1674db6bfe0SAlasdair G Kergon {
1684db6bfe0SAlasdair G Kergon 	int r = -ENOMEM;
1694db6bfe0SAlasdair G Kergon 	size_t len;
1704db6bfe0SAlasdair G Kergon 
17171fab00aSJonathan Brassow 	len = ps->store->chunk_size << SECTOR_SHIFT;
1724db6bfe0SAlasdair G Kergon 
1734db6bfe0SAlasdair G Kergon 	/*
1744db6bfe0SAlasdair G Kergon 	 * Allocate the chunk_size block of memory that will hold
1754db6bfe0SAlasdair G Kergon 	 * a single metadata area.
1764db6bfe0SAlasdair G Kergon 	 */
1774db6bfe0SAlasdair G Kergon 	ps->area = vmalloc(len);
1784db6bfe0SAlasdair G Kergon 	if (!ps->area)
17961578dcdSMikulas Patocka 		goto err_area;
1804db6bfe0SAlasdair G Kergon 
181e29e65aaSJoe Perches 	ps->zero_area = vzalloc(len);
18261578dcdSMikulas Patocka 	if (!ps->zero_area)
18361578dcdSMikulas Patocka 		goto err_zero_area;
1844db6bfe0SAlasdair G Kergon 
18561578dcdSMikulas Patocka 	ps->header_area = vmalloc(len);
18661578dcdSMikulas Patocka 	if (!ps->header_area)
18761578dcdSMikulas Patocka 		goto err_header_area;
18861578dcdSMikulas Patocka 
1894db6bfe0SAlasdair G Kergon 	return 0;
19061578dcdSMikulas Patocka 
19161578dcdSMikulas Patocka err_header_area:
19261578dcdSMikulas Patocka 	vfree(ps->zero_area);
19361578dcdSMikulas Patocka 
19461578dcdSMikulas Patocka err_zero_area:
19561578dcdSMikulas Patocka 	vfree(ps->area);
19661578dcdSMikulas Patocka 
19761578dcdSMikulas Patocka err_area:
19861578dcdSMikulas Patocka 	return r;
1994db6bfe0SAlasdair G Kergon }
2004db6bfe0SAlasdair G Kergon 
2014db6bfe0SAlasdair G Kergon static void free_area(struct pstore *ps)
2024db6bfe0SAlasdair G Kergon {
203a32079ceSJonathan Brassow 	if (ps->area)
2044db6bfe0SAlasdair G Kergon 		vfree(ps->area);
2054db6bfe0SAlasdair G Kergon 	ps->area = NULL;
206a32079ceSJonathan Brassow 
207a32079ceSJonathan Brassow 	if (ps->zero_area)
2084db6bfe0SAlasdair G Kergon 		vfree(ps->zero_area);
2094db6bfe0SAlasdair G Kergon 	ps->zero_area = NULL;
21061578dcdSMikulas Patocka 
21161578dcdSMikulas Patocka 	if (ps->header_area)
21261578dcdSMikulas Patocka 		vfree(ps->header_area);
21361578dcdSMikulas Patocka 	ps->header_area = NULL;
2144db6bfe0SAlasdair G Kergon }
2154db6bfe0SAlasdair G Kergon 
2164db6bfe0SAlasdair G Kergon struct mdata_req {
2174db6bfe0SAlasdair G Kergon 	struct dm_io_region *where;
2184db6bfe0SAlasdair G Kergon 	struct dm_io_request *io_req;
2194db6bfe0SAlasdair G Kergon 	struct work_struct work;
2204db6bfe0SAlasdair G Kergon 	int result;
2214db6bfe0SAlasdair G Kergon };
2224db6bfe0SAlasdair G Kergon 
2234db6bfe0SAlasdair G Kergon static void do_metadata(struct work_struct *work)
2244db6bfe0SAlasdair G Kergon {
2254db6bfe0SAlasdair G Kergon 	struct mdata_req *req = container_of(work, struct mdata_req, work);
2264db6bfe0SAlasdair G Kergon 
2274db6bfe0SAlasdair G Kergon 	req->result = dm_io(req->io_req, 1, req->where, NULL);
2284db6bfe0SAlasdair G Kergon }
2294db6bfe0SAlasdair G Kergon 
2304db6bfe0SAlasdair G Kergon /*
2314db6bfe0SAlasdair G Kergon  * Read or write a chunk aligned and sized block of data from a device.
2324db6bfe0SAlasdair G Kergon  */
23302d2fd31SMikulas Patocka static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, int rw,
23402d2fd31SMikulas Patocka 		    int metadata)
2354db6bfe0SAlasdair G Kergon {
2364db6bfe0SAlasdair G Kergon 	struct dm_io_region where = {
237fc56f6fbSMike Snitzer 		.bdev = dm_snap_cow(ps->store->snap)->bdev,
23871fab00aSJonathan Brassow 		.sector = ps->store->chunk_size * chunk,
23971fab00aSJonathan Brassow 		.count = ps->store->chunk_size,
2404db6bfe0SAlasdair G Kergon 	};
2414db6bfe0SAlasdair G Kergon 	struct dm_io_request io_req = {
2424db6bfe0SAlasdair G Kergon 		.bi_rw = rw,
2434db6bfe0SAlasdair G Kergon 		.mem.type = DM_IO_VMA,
24402d2fd31SMikulas Patocka 		.mem.ptr.vma = area,
2454db6bfe0SAlasdair G Kergon 		.client = ps->io_client,
2464db6bfe0SAlasdair G Kergon 		.notify.fn = NULL,
2474db6bfe0SAlasdair G Kergon 	};
2484db6bfe0SAlasdair G Kergon 	struct mdata_req req;
2494db6bfe0SAlasdair G Kergon 
2504db6bfe0SAlasdair G Kergon 	if (!metadata)
2514db6bfe0SAlasdair G Kergon 		return dm_io(&io_req, 1, &where, NULL);
2524db6bfe0SAlasdair G Kergon 
2534db6bfe0SAlasdair G Kergon 	req.where = &where;
2544db6bfe0SAlasdair G Kergon 	req.io_req = &io_req;
2554db6bfe0SAlasdair G Kergon 
2564db6bfe0SAlasdair G Kergon 	/*
2574db6bfe0SAlasdair G Kergon 	 * Issue the synchronous I/O from a different thread
2584db6bfe0SAlasdair G Kergon 	 * to avoid generic_make_request recursion.
2594db6bfe0SAlasdair G Kergon 	 */
260ca1cab37SAndrew Morton 	INIT_WORK_ONSTACK(&req.work, do_metadata);
2614db6bfe0SAlasdair G Kergon 	queue_work(ps->metadata_wq, &req.work);
2625ea330a7SMikulas Patocka 	flush_workqueue(ps->metadata_wq);
263c1a64160SChuansheng Liu 	destroy_work_on_stack(&req.work);
2644db6bfe0SAlasdair G Kergon 
2654db6bfe0SAlasdair G Kergon 	return req.result;
2664db6bfe0SAlasdair G Kergon }
2674db6bfe0SAlasdair G Kergon 
2684db6bfe0SAlasdair G Kergon /*
2694db6bfe0SAlasdair G Kergon  * Convert a metadata area index to a chunk index.
2704db6bfe0SAlasdair G Kergon  */
2714db6bfe0SAlasdair G Kergon static chunk_t area_location(struct pstore *ps, chunk_t area)
2724db6bfe0SAlasdair G Kergon {
27387c961cbSTomohiro Kusumi 	return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area);
2744db6bfe0SAlasdair G Kergon }
2754db6bfe0SAlasdair G Kergon 
276e9c6a182SMikulas Patocka static void skip_metadata(struct pstore *ps)
277e9c6a182SMikulas Patocka {
278e9c6a182SMikulas Patocka 	uint32_t stride = ps->exceptions_per_area + 1;
279e9c6a182SMikulas Patocka 	chunk_t next_free = ps->next_free;
280e9c6a182SMikulas Patocka 	if (sector_div(next_free, stride) == NUM_SNAPSHOT_HDR_CHUNKS)
281e9c6a182SMikulas Patocka 		ps->next_free++;
282e9c6a182SMikulas Patocka }
283e9c6a182SMikulas Patocka 
2844db6bfe0SAlasdair G Kergon /*
2854db6bfe0SAlasdair G Kergon  * Read or write a metadata area.  Remembering to skip the first
2864db6bfe0SAlasdair G Kergon  * chunk which holds the header.
2874db6bfe0SAlasdair G Kergon  */
2884db6bfe0SAlasdair G Kergon static int area_io(struct pstore *ps, int rw)
2894db6bfe0SAlasdair G Kergon {
2904db6bfe0SAlasdair G Kergon 	int r;
2914db6bfe0SAlasdair G Kergon 	chunk_t chunk;
2924db6bfe0SAlasdair G Kergon 
2934db6bfe0SAlasdair G Kergon 	chunk = area_location(ps, ps->current_area);
2944db6bfe0SAlasdair G Kergon 
29502d2fd31SMikulas Patocka 	r = chunk_io(ps, ps->area, chunk, rw, 0);
2964db6bfe0SAlasdair G Kergon 	if (r)
2974db6bfe0SAlasdair G Kergon 		return r;
2984db6bfe0SAlasdair G Kergon 
2994db6bfe0SAlasdair G Kergon 	return 0;
3004db6bfe0SAlasdair G Kergon }
3014db6bfe0SAlasdair G Kergon 
3024db6bfe0SAlasdair G Kergon static void zero_memory_area(struct pstore *ps)
3034db6bfe0SAlasdair G Kergon {
30471fab00aSJonathan Brassow 	memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
3054db6bfe0SAlasdair G Kergon }
3064db6bfe0SAlasdair G Kergon 
3074db6bfe0SAlasdair G Kergon static int zero_disk_area(struct pstore *ps, chunk_t area)
3084db6bfe0SAlasdair G Kergon {
30902d2fd31SMikulas Patocka 	return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
3104db6bfe0SAlasdair G Kergon }
3114db6bfe0SAlasdair G Kergon 
3124db6bfe0SAlasdair G Kergon static int read_header(struct pstore *ps, int *new_snapshot)
3134db6bfe0SAlasdair G Kergon {
3144db6bfe0SAlasdair G Kergon 	int r;
3154db6bfe0SAlasdair G Kergon 	struct disk_header *dh;
316df96eee6SMikulas Patocka 	unsigned chunk_size;
3174db6bfe0SAlasdair G Kergon 	int chunk_size_supplied = 1;
318ae0b7448SMikulas Patocka 	char *chunk_err;
3194db6bfe0SAlasdair G Kergon 
3204db6bfe0SAlasdair G Kergon 	/*
321df96eee6SMikulas Patocka 	 * Use default chunk size (or logical_block_size, if larger)
322df96eee6SMikulas Patocka 	 * if none supplied
3234db6bfe0SAlasdair G Kergon 	 */
32471fab00aSJonathan Brassow 	if (!ps->store->chunk_size) {
32571fab00aSJonathan Brassow 		ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
326fc56f6fbSMike Snitzer 		    bdev_logical_block_size(dm_snap_cow(ps->store->snap)->
327fc56f6fbSMike Snitzer 					    bdev) >> 9);
32871fab00aSJonathan Brassow 		ps->store->chunk_mask = ps->store->chunk_size - 1;
32971fab00aSJonathan Brassow 		ps->store->chunk_shift = ffs(ps->store->chunk_size) - 1;
3304db6bfe0SAlasdair G Kergon 		chunk_size_supplied = 0;
3314db6bfe0SAlasdair G Kergon 	}
3324db6bfe0SAlasdair G Kergon 
333bda8efecSMikulas Patocka 	ps->io_client = dm_io_client_create();
3344db6bfe0SAlasdair G Kergon 	if (IS_ERR(ps->io_client))
3354db6bfe0SAlasdair G Kergon 		return PTR_ERR(ps->io_client);
3364db6bfe0SAlasdair G Kergon 
3374db6bfe0SAlasdair G Kergon 	r = alloc_area(ps);
3384db6bfe0SAlasdair G Kergon 	if (r)
3394db6bfe0SAlasdair G Kergon 		return r;
3404db6bfe0SAlasdair G Kergon 
34161578dcdSMikulas Patocka 	r = chunk_io(ps, ps->header_area, 0, READ, 1);
3424db6bfe0SAlasdair G Kergon 	if (r)
3434db6bfe0SAlasdair G Kergon 		goto bad;
3444db6bfe0SAlasdair G Kergon 
34561578dcdSMikulas Patocka 	dh = ps->header_area;
3464db6bfe0SAlasdair G Kergon 
3474db6bfe0SAlasdair G Kergon 	if (le32_to_cpu(dh->magic) == 0) {
3484db6bfe0SAlasdair G Kergon 		*new_snapshot = 1;
3494db6bfe0SAlasdair G Kergon 		return 0;
3504db6bfe0SAlasdair G Kergon 	}
3514db6bfe0SAlasdair G Kergon 
3524db6bfe0SAlasdair G Kergon 	if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
3534db6bfe0SAlasdair G Kergon 		DMWARN("Invalid or corrupt snapshot");
3544db6bfe0SAlasdair G Kergon 		r = -ENXIO;
3554db6bfe0SAlasdair G Kergon 		goto bad;
3564db6bfe0SAlasdair G Kergon 	}
3574db6bfe0SAlasdair G Kergon 
3584db6bfe0SAlasdair G Kergon 	*new_snapshot = 0;
3594db6bfe0SAlasdair G Kergon 	ps->valid = le32_to_cpu(dh->valid);
3604db6bfe0SAlasdair G Kergon 	ps->version = le32_to_cpu(dh->version);
3614db6bfe0SAlasdair G Kergon 	chunk_size = le32_to_cpu(dh->chunk_size);
3624db6bfe0SAlasdair G Kergon 
363ae0b7448SMikulas Patocka 	if (ps->store->chunk_size == chunk_size)
3644db6bfe0SAlasdair G Kergon 		return 0;
3654db6bfe0SAlasdair G Kergon 
366ae0b7448SMikulas Patocka 	if (chunk_size_supplied)
367df96eee6SMikulas Patocka 		DMWARN("chunk size %u in device metadata overrides "
368df96eee6SMikulas Patocka 		       "table chunk size of %u.",
369df96eee6SMikulas Patocka 		       chunk_size, ps->store->chunk_size);
3704db6bfe0SAlasdair G Kergon 
3714db6bfe0SAlasdair G Kergon 	/* We had a bogus chunk_size. Fix stuff up. */
3724db6bfe0SAlasdair G Kergon 	free_area(ps);
3734db6bfe0SAlasdair G Kergon 
374ae0b7448SMikulas Patocka 	r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
375ae0b7448SMikulas Patocka 					      &chunk_err);
376ae0b7448SMikulas Patocka 	if (r) {
377df96eee6SMikulas Patocka 		DMERR("invalid on-disk chunk size %u: %s.",
378df96eee6SMikulas Patocka 		      chunk_size, chunk_err);
379ae0b7448SMikulas Patocka 		return r;
380ae0b7448SMikulas Patocka 	}
3814db6bfe0SAlasdair G Kergon 
3824db6bfe0SAlasdair G Kergon 	r = alloc_area(ps);
3834db6bfe0SAlasdair G Kergon 	return r;
3844db6bfe0SAlasdair G Kergon 
3854db6bfe0SAlasdair G Kergon bad:
3864db6bfe0SAlasdair G Kergon 	free_area(ps);
3874db6bfe0SAlasdair G Kergon 	return r;
3884db6bfe0SAlasdair G Kergon }
3894db6bfe0SAlasdair G Kergon 
3904db6bfe0SAlasdair G Kergon static int write_header(struct pstore *ps)
3914db6bfe0SAlasdair G Kergon {
3924db6bfe0SAlasdair G Kergon 	struct disk_header *dh;
3934db6bfe0SAlasdair G Kergon 
39461578dcdSMikulas Patocka 	memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
3954db6bfe0SAlasdair G Kergon 
39661578dcdSMikulas Patocka 	dh = ps->header_area;
3974db6bfe0SAlasdair G Kergon 	dh->magic = cpu_to_le32(SNAP_MAGIC);
3984db6bfe0SAlasdair G Kergon 	dh->valid = cpu_to_le32(ps->valid);
3994db6bfe0SAlasdair G Kergon 	dh->version = cpu_to_le32(ps->version);
40071fab00aSJonathan Brassow 	dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
4014db6bfe0SAlasdair G Kergon 
40261578dcdSMikulas Patocka 	return chunk_io(ps, ps->header_area, 0, WRITE, 1);
4034db6bfe0SAlasdair G Kergon }
4044db6bfe0SAlasdair G Kergon 
4054db6bfe0SAlasdair G Kergon /*
4064db6bfe0SAlasdair G Kergon  * Access functions for the disk exceptions, these do the endian conversions.
4074db6bfe0SAlasdair G Kergon  */
4082cadabd5SMikulas Patocka static struct disk_exception *get_exception(struct pstore *ps, void *ps_area,
4092cadabd5SMikulas Patocka 					    uint32_t index)
4104db6bfe0SAlasdair G Kergon {
4114db6bfe0SAlasdair G Kergon 	BUG_ON(index >= ps->exceptions_per_area);
4124db6bfe0SAlasdair G Kergon 
4132cadabd5SMikulas Patocka 	return ((struct disk_exception *) ps_area) + index;
4144db6bfe0SAlasdair G Kergon }
4154db6bfe0SAlasdair G Kergon 
4162cadabd5SMikulas Patocka static void read_exception(struct pstore *ps, void *ps_area,
417283a8328SAlasdair G Kergon 			   uint32_t index, struct core_exception *result)
4184db6bfe0SAlasdair G Kergon {
4192cadabd5SMikulas Patocka 	struct disk_exception *de = get_exception(ps, ps_area, index);
4204db6bfe0SAlasdair G Kergon 
4214db6bfe0SAlasdair G Kergon 	/* copy it */
422283a8328SAlasdair G Kergon 	result->old_chunk = le64_to_cpu(de->old_chunk);
423283a8328SAlasdair G Kergon 	result->new_chunk = le64_to_cpu(de->new_chunk);
4244db6bfe0SAlasdair G Kergon }
4254db6bfe0SAlasdair G Kergon 
4264db6bfe0SAlasdair G Kergon static void write_exception(struct pstore *ps,
427283a8328SAlasdair G Kergon 			    uint32_t index, struct core_exception *e)
4284db6bfe0SAlasdair G Kergon {
4292cadabd5SMikulas Patocka 	struct disk_exception *de = get_exception(ps, ps->area, index);
4304db6bfe0SAlasdair G Kergon 
4314db6bfe0SAlasdair G Kergon 	/* copy it */
432283a8328SAlasdair G Kergon 	de->old_chunk = cpu_to_le64(e->old_chunk);
433283a8328SAlasdair G Kergon 	de->new_chunk = cpu_to_le64(e->new_chunk);
4344db6bfe0SAlasdair G Kergon }
4354db6bfe0SAlasdair G Kergon 
4364454a621SMikulas Patocka static void clear_exception(struct pstore *ps, uint32_t index)
4374454a621SMikulas Patocka {
4382cadabd5SMikulas Patocka 	struct disk_exception *de = get_exception(ps, ps->area, index);
4394454a621SMikulas Patocka 
4404454a621SMikulas Patocka 	/* clear it */
441283a8328SAlasdair G Kergon 	de->old_chunk = 0;
442283a8328SAlasdair G Kergon 	de->new_chunk = 0;
4434454a621SMikulas Patocka }
4444454a621SMikulas Patocka 
4454db6bfe0SAlasdair G Kergon /*
4464db6bfe0SAlasdair G Kergon  * Registers the exceptions that are present in the current area.
4474db6bfe0SAlasdair G Kergon  * 'full' is filled in to indicate if the area has been
4484db6bfe0SAlasdair G Kergon  * filled.
4494db6bfe0SAlasdair G Kergon  */
4502cadabd5SMikulas Patocka static int insert_exceptions(struct pstore *ps, void *ps_area,
451a159c1acSJonathan Brassow 			     int (*callback)(void *callback_context,
452a159c1acSJonathan Brassow 					     chunk_t old, chunk_t new),
453a159c1acSJonathan Brassow 			     void *callback_context,
454a159c1acSJonathan Brassow 			     int *full)
4554db6bfe0SAlasdair G Kergon {
4564db6bfe0SAlasdair G Kergon 	int r;
4574db6bfe0SAlasdair G Kergon 	unsigned int i;
458283a8328SAlasdair G Kergon 	struct core_exception e;
4594db6bfe0SAlasdair G Kergon 
4604db6bfe0SAlasdair G Kergon 	/* presume the area is full */
4614db6bfe0SAlasdair G Kergon 	*full = 1;
4624db6bfe0SAlasdair G Kergon 
4634db6bfe0SAlasdair G Kergon 	for (i = 0; i < ps->exceptions_per_area; i++) {
4642cadabd5SMikulas Patocka 		read_exception(ps, ps_area, i, &e);
4654db6bfe0SAlasdair G Kergon 
4664db6bfe0SAlasdair G Kergon 		/*
4674db6bfe0SAlasdair G Kergon 		 * If the new_chunk is pointing at the start of
4684db6bfe0SAlasdair G Kergon 		 * the COW device, where the first metadata area
4694db6bfe0SAlasdair G Kergon 		 * is we know that we've hit the end of the
4704db6bfe0SAlasdair G Kergon 		 * exceptions.  Therefore the area is not full.
4714db6bfe0SAlasdair G Kergon 		 */
472283a8328SAlasdair G Kergon 		if (e.new_chunk == 0LL) {
4734db6bfe0SAlasdair G Kergon 			ps->current_committed = i;
4744db6bfe0SAlasdair G Kergon 			*full = 0;
4754db6bfe0SAlasdair G Kergon 			break;
4764db6bfe0SAlasdair G Kergon 		}
4774db6bfe0SAlasdair G Kergon 
4784db6bfe0SAlasdair G Kergon 		/*
4794db6bfe0SAlasdair G Kergon 		 * Keep track of the start of the free chunks.
4804db6bfe0SAlasdair G Kergon 		 */
481283a8328SAlasdair G Kergon 		if (ps->next_free <= e.new_chunk)
482283a8328SAlasdair G Kergon 			ps->next_free = e.new_chunk + 1;
4834db6bfe0SAlasdair G Kergon 
4844db6bfe0SAlasdair G Kergon 		/*
4854db6bfe0SAlasdair G Kergon 		 * Otherwise we add the exception to the snapshot.
4864db6bfe0SAlasdair G Kergon 		 */
487283a8328SAlasdair G Kergon 		r = callback(callback_context, e.old_chunk, e.new_chunk);
4884db6bfe0SAlasdair G Kergon 		if (r)
4894db6bfe0SAlasdair G Kergon 			return r;
4904db6bfe0SAlasdair G Kergon 	}
4914db6bfe0SAlasdair G Kergon 
4924db6bfe0SAlasdair G Kergon 	return 0;
4934db6bfe0SAlasdair G Kergon }
4944db6bfe0SAlasdair G Kergon 
495a159c1acSJonathan Brassow static int read_exceptions(struct pstore *ps,
496a159c1acSJonathan Brassow 			   int (*callback)(void *callback_context, chunk_t old,
497a159c1acSJonathan Brassow 					   chunk_t new),
498a159c1acSJonathan Brassow 			   void *callback_context)
4994db6bfe0SAlasdair G Kergon {
5004db6bfe0SAlasdair G Kergon 	int r, full = 1;
50155494bf2SMikulas Patocka 	struct dm_bufio_client *client;
502*55b082e6SMikulas Patocka 	chunk_t prefetch_area = 0;
50355494bf2SMikulas Patocka 
50455494bf2SMikulas Patocka 	client = dm_bufio_client_create(dm_snap_cow(ps->store->snap)->bdev,
50555494bf2SMikulas Patocka 					ps->store->chunk_size << SECTOR_SHIFT,
50655494bf2SMikulas Patocka 					1, 0, NULL, NULL);
50755494bf2SMikulas Patocka 
50855494bf2SMikulas Patocka 	if (IS_ERR(client))
50955494bf2SMikulas Patocka 		return PTR_ERR(client);
5104db6bfe0SAlasdair G Kergon 
5114db6bfe0SAlasdair G Kergon 	/*
512*55b082e6SMikulas Patocka 	 * Setup for one current buffer + desired readahead buffers.
513*55b082e6SMikulas Patocka 	 */
514*55b082e6SMikulas Patocka 	dm_bufio_set_minimum_buffers(client, 1 + DM_PREFETCH_CHUNKS);
515*55b082e6SMikulas Patocka 
516*55b082e6SMikulas Patocka 	/*
5174db6bfe0SAlasdair G Kergon 	 * Keeping reading chunks and inserting exceptions until
5184db6bfe0SAlasdair G Kergon 	 * we find a partially full area.
5194db6bfe0SAlasdair G Kergon 	 */
5204db6bfe0SAlasdair G Kergon 	for (ps->current_area = 0; full; ps->current_area++) {
52155494bf2SMikulas Patocka 		struct dm_buffer *bp;
52255494bf2SMikulas Patocka 		void *area;
523*55b082e6SMikulas Patocka 		chunk_t chunk;
524*55b082e6SMikulas Patocka 
525*55b082e6SMikulas Patocka 		if (unlikely(prefetch_area < ps->current_area))
526*55b082e6SMikulas Patocka 			prefetch_area = ps->current_area;
527*55b082e6SMikulas Patocka 
528*55b082e6SMikulas Patocka 		if (DM_PREFETCH_CHUNKS) do {
529*55b082e6SMikulas Patocka 			chunk_t pf_chunk = area_location(ps, prefetch_area);
530*55b082e6SMikulas Patocka 			if (unlikely(pf_chunk >= dm_bufio_get_device_size(client)))
531*55b082e6SMikulas Patocka 				break;
532*55b082e6SMikulas Patocka 			dm_bufio_prefetch(client, pf_chunk, 1);
533*55b082e6SMikulas Patocka 			prefetch_area++;
534*55b082e6SMikulas Patocka 			if (unlikely(!prefetch_area))
535*55b082e6SMikulas Patocka 				break;
536*55b082e6SMikulas Patocka 		} while (prefetch_area <= ps->current_area + DM_PREFETCH_CHUNKS);
537*55b082e6SMikulas Patocka 
538*55b082e6SMikulas Patocka 		chunk = area_location(ps, ps->current_area);
5394db6bfe0SAlasdair G Kergon 
54055494bf2SMikulas Patocka 		area = dm_bufio_read(client, chunk, &bp);
54155494bf2SMikulas Patocka 		if (unlikely(IS_ERR(area))) {
54255494bf2SMikulas Patocka 			r = PTR_ERR(area);
54355494bf2SMikulas Patocka 			goto ret_destroy_bufio;
54455494bf2SMikulas Patocka 		}
54555494bf2SMikulas Patocka 
54655494bf2SMikulas Patocka 		r = insert_exceptions(ps, area, callback, callback_context,
5472cadabd5SMikulas Patocka 				      &full);
54855494bf2SMikulas Patocka 
54955494bf2SMikulas Patocka 		dm_bufio_release(bp);
55055494bf2SMikulas Patocka 
55155494bf2SMikulas Patocka 		dm_bufio_forget(client, chunk);
55255494bf2SMikulas Patocka 
55355494bf2SMikulas Patocka 		if (unlikely(r))
55455494bf2SMikulas Patocka 			goto ret_destroy_bufio;
5554db6bfe0SAlasdair G Kergon 	}
5564db6bfe0SAlasdair G Kergon 
5574db6bfe0SAlasdair G Kergon 	ps->current_area--;
5584db6bfe0SAlasdair G Kergon 
559e9c6a182SMikulas Patocka 	skip_metadata(ps);
560e9c6a182SMikulas Patocka 
56155494bf2SMikulas Patocka 	r = 0;
56255494bf2SMikulas Patocka 
56355494bf2SMikulas Patocka ret_destroy_bufio:
56455494bf2SMikulas Patocka 	dm_bufio_client_destroy(client);
56555494bf2SMikulas Patocka 
56655494bf2SMikulas Patocka 	return r;
5674db6bfe0SAlasdair G Kergon }
5684db6bfe0SAlasdair G Kergon 
5694db6bfe0SAlasdair G Kergon static struct pstore *get_info(struct dm_exception_store *store)
5704db6bfe0SAlasdair G Kergon {
5714db6bfe0SAlasdair G Kergon 	return (struct pstore *) store->context;
5724db6bfe0SAlasdair G Kergon }
5734db6bfe0SAlasdair G Kergon 
574985903bbSMike Snitzer static void persistent_usage(struct dm_exception_store *store,
575985903bbSMike Snitzer 			     sector_t *total_sectors,
576985903bbSMike Snitzer 			     sector_t *sectors_allocated,
577985903bbSMike Snitzer 			     sector_t *metadata_sectors)
5784db6bfe0SAlasdair G Kergon {
579985903bbSMike Snitzer 	struct pstore *ps = get_info(store);
580985903bbSMike Snitzer 
581985903bbSMike Snitzer 	*sectors_allocated = ps->next_free * store->chunk_size;
582fc56f6fbSMike Snitzer 	*total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
583985903bbSMike Snitzer 
584985903bbSMike Snitzer 	/*
585985903bbSMike Snitzer 	 * First chunk is the fixed header.
586985903bbSMike Snitzer 	 * Then there are (ps->current_area + 1) metadata chunks, each one
587985903bbSMike Snitzer 	 * separated from the next by ps->exceptions_per_area data chunks.
588985903bbSMike Snitzer 	 */
5894454a621SMikulas Patocka 	*metadata_sectors = (ps->current_area + 1 + NUM_SNAPSHOT_HDR_CHUNKS) *
5904454a621SMikulas Patocka 			    store->chunk_size;
5914db6bfe0SAlasdair G Kergon }
5924db6bfe0SAlasdair G Kergon 
593493df71cSJonathan Brassow static void persistent_dtr(struct dm_exception_store *store)
5944db6bfe0SAlasdair G Kergon {
5954db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
5964db6bfe0SAlasdair G Kergon 
5974db6bfe0SAlasdair G Kergon 	destroy_workqueue(ps->metadata_wq);
598a32079ceSJonathan Brassow 
599a32079ceSJonathan Brassow 	/* Created in read_header */
600a32079ceSJonathan Brassow 	if (ps->io_client)
6014db6bfe0SAlasdair G Kergon 		dm_io_client_destroy(ps->io_client);
6024db6bfe0SAlasdair G Kergon 	free_area(ps);
603a32079ceSJonathan Brassow 
604a32079ceSJonathan Brassow 	/* Allocated in persistent_read_metadata */
605a32079ceSJonathan Brassow 	if (ps->callbacks)
606a32079ceSJonathan Brassow 		vfree(ps->callbacks);
607a32079ceSJonathan Brassow 
6084db6bfe0SAlasdair G Kergon 	kfree(ps);
6094db6bfe0SAlasdair G Kergon }
6104db6bfe0SAlasdair G Kergon 
611a159c1acSJonathan Brassow static int persistent_read_metadata(struct dm_exception_store *store,
612a159c1acSJonathan Brassow 				    int (*callback)(void *callback_context,
613a159c1acSJonathan Brassow 						    chunk_t old, chunk_t new),
614a159c1acSJonathan Brassow 				    void *callback_context)
6154db6bfe0SAlasdair G Kergon {
6164db6bfe0SAlasdair G Kergon 	int r, uninitialized_var(new_snapshot);
6174db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
6184db6bfe0SAlasdair G Kergon 
6194db6bfe0SAlasdair G Kergon 	/*
6204db6bfe0SAlasdair G Kergon 	 * Read the snapshot header.
6214db6bfe0SAlasdair G Kergon 	 */
6224db6bfe0SAlasdair G Kergon 	r = read_header(ps, &new_snapshot);
6234db6bfe0SAlasdair G Kergon 	if (r)
6244db6bfe0SAlasdair G Kergon 		return r;
6254db6bfe0SAlasdair G Kergon 
6264db6bfe0SAlasdair G Kergon 	/*
6274db6bfe0SAlasdair G Kergon 	 * Now we know correct chunk_size, complete the initialisation.
6284db6bfe0SAlasdair G Kergon 	 */
62971fab00aSJonathan Brassow 	ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
63071fab00aSJonathan Brassow 				  sizeof(struct disk_exception);
6314db6bfe0SAlasdair G Kergon 	ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
6324db6bfe0SAlasdair G Kergon 				   sizeof(*ps->callbacks));
6334db6bfe0SAlasdair G Kergon 	if (!ps->callbacks)
6344db6bfe0SAlasdair G Kergon 		return -ENOMEM;
6354db6bfe0SAlasdair G Kergon 
6364db6bfe0SAlasdair G Kergon 	/*
6374db6bfe0SAlasdair G Kergon 	 * Do we need to setup a new snapshot ?
6384db6bfe0SAlasdair G Kergon 	 */
6394db6bfe0SAlasdair G Kergon 	if (new_snapshot) {
6404db6bfe0SAlasdair G Kergon 		r = write_header(ps);
6414db6bfe0SAlasdair G Kergon 		if (r) {
6424db6bfe0SAlasdair G Kergon 			DMWARN("write_header failed");
6434db6bfe0SAlasdair G Kergon 			return r;
6444db6bfe0SAlasdair G Kergon 		}
6454db6bfe0SAlasdair G Kergon 
6464db6bfe0SAlasdair G Kergon 		ps->current_area = 0;
6474db6bfe0SAlasdair G Kergon 		zero_memory_area(ps);
6484db6bfe0SAlasdair G Kergon 		r = zero_disk_area(ps, 0);
649f5acc834SJon Brassow 		if (r)
6504db6bfe0SAlasdair G Kergon 			DMWARN("zero_disk_area(0) failed");
6514db6bfe0SAlasdair G Kergon 		return r;
6524db6bfe0SAlasdair G Kergon 	}
6534db6bfe0SAlasdair G Kergon 	/*
6544db6bfe0SAlasdair G Kergon 	 * Sanity checks.
6554db6bfe0SAlasdair G Kergon 	 */
6564db6bfe0SAlasdair G Kergon 	if (ps->version != SNAPSHOT_DISK_VERSION) {
6574db6bfe0SAlasdair G Kergon 		DMWARN("unable to handle snapshot disk version %d",
6584db6bfe0SAlasdair G Kergon 		       ps->version);
6594db6bfe0SAlasdair G Kergon 		return -EINVAL;
6604db6bfe0SAlasdair G Kergon 	}
6614db6bfe0SAlasdair G Kergon 
6624db6bfe0SAlasdair G Kergon 	/*
6634db6bfe0SAlasdair G Kergon 	 * Metadata are valid, but snapshot is invalidated
6644db6bfe0SAlasdair G Kergon 	 */
6654db6bfe0SAlasdair G Kergon 	if (!ps->valid)
6664db6bfe0SAlasdair G Kergon 		return 1;
6674db6bfe0SAlasdair G Kergon 
6684db6bfe0SAlasdair G Kergon 	/*
6694db6bfe0SAlasdair G Kergon 	 * Read the metadata.
6704db6bfe0SAlasdair G Kergon 	 */
671a159c1acSJonathan Brassow 	r = read_exceptions(ps, callback, callback_context);
6724db6bfe0SAlasdair G Kergon 
673f5acc834SJon Brassow 	return r;
6744db6bfe0SAlasdair G Kergon }
6754db6bfe0SAlasdair G Kergon 
676a159c1acSJonathan Brassow static int persistent_prepare_exception(struct dm_exception_store *store,
6771d4989c8SJon Brassow 					struct dm_exception *e)
6784db6bfe0SAlasdair G Kergon {
6794db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
680fc56f6fbSMike Snitzer 	sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
6814db6bfe0SAlasdair G Kergon 
6824db6bfe0SAlasdair G Kergon 	/* Is there enough room ? */
683d0216849SJonathan Brassow 	if (size < ((ps->next_free + 1) * store->chunk_size))
6844db6bfe0SAlasdair G Kergon 		return -ENOSPC;
6854db6bfe0SAlasdair G Kergon 
6864db6bfe0SAlasdair G Kergon 	e->new_chunk = ps->next_free;
6874db6bfe0SAlasdair G Kergon 
6884db6bfe0SAlasdair G Kergon 	/*
6894db6bfe0SAlasdair G Kergon 	 * Move onto the next free pending, making sure to take
6904db6bfe0SAlasdair G Kergon 	 * into account the location of the metadata chunks.
6914db6bfe0SAlasdair G Kergon 	 */
6924db6bfe0SAlasdair G Kergon 	ps->next_free++;
693e9c6a182SMikulas Patocka 	skip_metadata(ps);
6944db6bfe0SAlasdair G Kergon 
6954db6bfe0SAlasdair G Kergon 	atomic_inc(&ps->pending_count);
6964db6bfe0SAlasdair G Kergon 	return 0;
6974db6bfe0SAlasdair G Kergon }
6984db6bfe0SAlasdair G Kergon 
699a159c1acSJonathan Brassow static void persistent_commit_exception(struct dm_exception_store *store,
7001d4989c8SJon Brassow 					struct dm_exception *e,
7014db6bfe0SAlasdair G Kergon 					void (*callback) (void *, int success),
7024db6bfe0SAlasdair G Kergon 					void *callback_context)
7034db6bfe0SAlasdair G Kergon {
7044db6bfe0SAlasdair G Kergon 	unsigned int i;
7054db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
706283a8328SAlasdair G Kergon 	struct core_exception ce;
7074db6bfe0SAlasdair G Kergon 	struct commit_callback *cb;
7084db6bfe0SAlasdair G Kergon 
709283a8328SAlasdair G Kergon 	ce.old_chunk = e->old_chunk;
710283a8328SAlasdair G Kergon 	ce.new_chunk = e->new_chunk;
711283a8328SAlasdair G Kergon 	write_exception(ps, ps->current_committed++, &ce);
7124db6bfe0SAlasdair G Kergon 
7134db6bfe0SAlasdair G Kergon 	/*
7144db6bfe0SAlasdair G Kergon 	 * Add the callback to the back of the array.  This code
7154db6bfe0SAlasdair G Kergon 	 * is the only place where the callback array is
7164db6bfe0SAlasdair G Kergon 	 * manipulated, and we know that it will never be called
7174db6bfe0SAlasdair G Kergon 	 * multiple times concurrently.
7184db6bfe0SAlasdair G Kergon 	 */
7194db6bfe0SAlasdair G Kergon 	cb = ps->callbacks + ps->callback_count++;
7204db6bfe0SAlasdair G Kergon 	cb->callback = callback;
7214db6bfe0SAlasdair G Kergon 	cb->context = callback_context;
7224db6bfe0SAlasdair G Kergon 
7234db6bfe0SAlasdair G Kergon 	/*
7244db6bfe0SAlasdair G Kergon 	 * If there are exceptions in flight and we have not yet
7254db6bfe0SAlasdair G Kergon 	 * filled this metadata area there's nothing more to do.
7264db6bfe0SAlasdair G Kergon 	 */
7274db6bfe0SAlasdair G Kergon 	if (!atomic_dec_and_test(&ps->pending_count) &&
7284db6bfe0SAlasdair G Kergon 	    (ps->current_committed != ps->exceptions_per_area))
7294db6bfe0SAlasdair G Kergon 		return;
7304db6bfe0SAlasdair G Kergon 
7314db6bfe0SAlasdair G Kergon 	/*
7324db6bfe0SAlasdair G Kergon 	 * If we completely filled the current area, then wipe the next one.
7334db6bfe0SAlasdair G Kergon 	 */
7344db6bfe0SAlasdair G Kergon 	if ((ps->current_committed == ps->exceptions_per_area) &&
7354db6bfe0SAlasdair G Kergon 	    zero_disk_area(ps, ps->current_area + 1))
7364db6bfe0SAlasdair G Kergon 		ps->valid = 0;
7374db6bfe0SAlasdair G Kergon 
7384db6bfe0SAlasdair G Kergon 	/*
7394db6bfe0SAlasdair G Kergon 	 * Commit exceptions to disk.
7404db6bfe0SAlasdair G Kergon 	 */
741d87f4c14STejun Heo 	if (ps->valid && area_io(ps, WRITE_FLUSH_FUA))
7424db6bfe0SAlasdair G Kergon 		ps->valid = 0;
7434db6bfe0SAlasdair G Kergon 
7444db6bfe0SAlasdair G Kergon 	/*
7454db6bfe0SAlasdair G Kergon 	 * Advance to the next area if this one is full.
7464db6bfe0SAlasdair G Kergon 	 */
7474db6bfe0SAlasdair G Kergon 	if (ps->current_committed == ps->exceptions_per_area) {
7484db6bfe0SAlasdair G Kergon 		ps->current_committed = 0;
7494db6bfe0SAlasdair G Kergon 		ps->current_area++;
7504db6bfe0SAlasdair G Kergon 		zero_memory_area(ps);
7514db6bfe0SAlasdair G Kergon 	}
7524db6bfe0SAlasdair G Kergon 
7534db6bfe0SAlasdair G Kergon 	for (i = 0; i < ps->callback_count; i++) {
7544db6bfe0SAlasdair G Kergon 		cb = ps->callbacks + i;
7554db6bfe0SAlasdair G Kergon 		cb->callback(cb->context, ps->valid);
7564db6bfe0SAlasdair G Kergon 	}
7574db6bfe0SAlasdair G Kergon 
7584db6bfe0SAlasdair G Kergon 	ps->callback_count = 0;
7594db6bfe0SAlasdair G Kergon }
7604db6bfe0SAlasdair G Kergon 
7614454a621SMikulas Patocka static int persistent_prepare_merge(struct dm_exception_store *store,
7624454a621SMikulas Patocka 				    chunk_t *last_old_chunk,
7634454a621SMikulas Patocka 				    chunk_t *last_new_chunk)
7644454a621SMikulas Patocka {
7654454a621SMikulas Patocka 	struct pstore *ps = get_info(store);
766283a8328SAlasdair G Kergon 	struct core_exception ce;
7674454a621SMikulas Patocka 	int nr_consecutive;
7684454a621SMikulas Patocka 	int r;
7694454a621SMikulas Patocka 
7704454a621SMikulas Patocka 	/*
7714454a621SMikulas Patocka 	 * When current area is empty, move back to preceding area.
7724454a621SMikulas Patocka 	 */
7734454a621SMikulas Patocka 	if (!ps->current_committed) {
7744454a621SMikulas Patocka 		/*
7754454a621SMikulas Patocka 		 * Have we finished?
7764454a621SMikulas Patocka 		 */
7774454a621SMikulas Patocka 		if (!ps->current_area)
7784454a621SMikulas Patocka 			return 0;
7794454a621SMikulas Patocka 
7804454a621SMikulas Patocka 		ps->current_area--;
7814454a621SMikulas Patocka 		r = area_io(ps, READ);
7824454a621SMikulas Patocka 		if (r < 0)
7834454a621SMikulas Patocka 			return r;
7844454a621SMikulas Patocka 		ps->current_committed = ps->exceptions_per_area;
7854454a621SMikulas Patocka 	}
7864454a621SMikulas Patocka 
7872cadabd5SMikulas Patocka 	read_exception(ps, ps->area, ps->current_committed - 1, &ce);
788283a8328SAlasdair G Kergon 	*last_old_chunk = ce.old_chunk;
789283a8328SAlasdair G Kergon 	*last_new_chunk = ce.new_chunk;
7904454a621SMikulas Patocka 
7914454a621SMikulas Patocka 	/*
7924454a621SMikulas Patocka 	 * Find number of consecutive chunks within the current area,
7934454a621SMikulas Patocka 	 * working backwards.
7944454a621SMikulas Patocka 	 */
7954454a621SMikulas Patocka 	for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
7964454a621SMikulas Patocka 	     nr_consecutive++) {
7972cadabd5SMikulas Patocka 		read_exception(ps, ps->area,
7982cadabd5SMikulas Patocka 			       ps->current_committed - 1 - nr_consecutive, &ce);
799283a8328SAlasdair G Kergon 		if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
800283a8328SAlasdair G Kergon 		    ce.new_chunk != *last_new_chunk - nr_consecutive)
8014454a621SMikulas Patocka 			break;
8024454a621SMikulas Patocka 	}
8034454a621SMikulas Patocka 
8044454a621SMikulas Patocka 	return nr_consecutive;
8054454a621SMikulas Patocka }
8064454a621SMikulas Patocka 
8074454a621SMikulas Patocka static int persistent_commit_merge(struct dm_exception_store *store,
8084454a621SMikulas Patocka 				   int nr_merged)
8094454a621SMikulas Patocka {
8104454a621SMikulas Patocka 	int r, i;
8114454a621SMikulas Patocka 	struct pstore *ps = get_info(store);
8124454a621SMikulas Patocka 
8134454a621SMikulas Patocka 	BUG_ON(nr_merged > ps->current_committed);
8144454a621SMikulas Patocka 
8154454a621SMikulas Patocka 	for (i = 0; i < nr_merged; i++)
8164454a621SMikulas Patocka 		clear_exception(ps, ps->current_committed - 1 - i);
8174454a621SMikulas Patocka 
818762a80d9SMikulas Patocka 	r = area_io(ps, WRITE_FLUSH_FUA);
8194454a621SMikulas Patocka 	if (r < 0)
8204454a621SMikulas Patocka 		return r;
8214454a621SMikulas Patocka 
8224454a621SMikulas Patocka 	ps->current_committed -= nr_merged;
8234454a621SMikulas Patocka 
8244454a621SMikulas Patocka 	/*
8254454a621SMikulas Patocka 	 * At this stage, only persistent_usage() uses ps->next_free, so
8264454a621SMikulas Patocka 	 * we make no attempt to keep ps->next_free strictly accurate
8274454a621SMikulas Patocka 	 * as exceptions may have been committed out-of-order originally.
8284454a621SMikulas Patocka 	 * Once a snapshot has become merging, we set it to the value it
8294454a621SMikulas Patocka 	 * would have held had all the exceptions been committed in order.
8304454a621SMikulas Patocka 	 *
8314454a621SMikulas Patocka 	 * ps->current_area does not get reduced by prepare_merge() until
8324454a621SMikulas Patocka 	 * after commit_merge() has removed the nr_merged previous exceptions.
8334454a621SMikulas Patocka 	 */
83487c961cbSTomohiro Kusumi 	ps->next_free = area_location(ps, ps->current_area) +
83587c961cbSTomohiro Kusumi 			ps->current_committed + 1;
8364454a621SMikulas Patocka 
8374454a621SMikulas Patocka 	return 0;
8384454a621SMikulas Patocka }
8394454a621SMikulas Patocka 
840a159c1acSJonathan Brassow static void persistent_drop_snapshot(struct dm_exception_store *store)
8414db6bfe0SAlasdair G Kergon {
8424db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
8434db6bfe0SAlasdair G Kergon 
8444db6bfe0SAlasdair G Kergon 	ps->valid = 0;
8454db6bfe0SAlasdair G Kergon 	if (write_header(ps))
8464db6bfe0SAlasdair G Kergon 		DMWARN("write header failed");
8474db6bfe0SAlasdair G Kergon }
8484db6bfe0SAlasdair G Kergon 
849493df71cSJonathan Brassow static int persistent_ctr(struct dm_exception_store *store,
850493df71cSJonathan Brassow 			  unsigned argc, char **argv)
8514db6bfe0SAlasdair G Kergon {
8524db6bfe0SAlasdair G Kergon 	struct pstore *ps;
8534db6bfe0SAlasdair G Kergon 
8544db6bfe0SAlasdair G Kergon 	/* allocate the pstore */
855a32079ceSJonathan Brassow 	ps = kzalloc(sizeof(*ps), GFP_KERNEL);
8564db6bfe0SAlasdair G Kergon 	if (!ps)
8574db6bfe0SAlasdair G Kergon 		return -ENOMEM;
8584db6bfe0SAlasdair G Kergon 
85971fab00aSJonathan Brassow 	ps->store = store;
8604db6bfe0SAlasdair G Kergon 	ps->valid = 1;
8614db6bfe0SAlasdair G Kergon 	ps->version = SNAPSHOT_DISK_VERSION;
8624db6bfe0SAlasdair G Kergon 	ps->area = NULL;
86361578dcdSMikulas Patocka 	ps->zero_area = NULL;
86461578dcdSMikulas Patocka 	ps->header_area = NULL;
8654454a621SMikulas Patocka 	ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
8664db6bfe0SAlasdair G Kergon 	ps->current_committed = 0;
8674db6bfe0SAlasdair G Kergon 
8684db6bfe0SAlasdair G Kergon 	ps->callback_count = 0;
8694db6bfe0SAlasdair G Kergon 	atomic_set(&ps->pending_count, 0);
8704db6bfe0SAlasdair G Kergon 	ps->callbacks = NULL;
8714db6bfe0SAlasdair G Kergon 
872239c8dd5STejun Heo 	ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0);
8734db6bfe0SAlasdair G Kergon 	if (!ps->metadata_wq) {
8744db6bfe0SAlasdair G Kergon 		kfree(ps);
8754db6bfe0SAlasdair G Kergon 		DMERR("couldn't start header metadata update thread");
8764db6bfe0SAlasdair G Kergon 		return -ENOMEM;
8774db6bfe0SAlasdair G Kergon 	}
8784db6bfe0SAlasdair G Kergon 
8794db6bfe0SAlasdair G Kergon 	store->context = ps;
8804db6bfe0SAlasdair G Kergon 
8814db6bfe0SAlasdair G Kergon 	return 0;
8824db6bfe0SAlasdair G Kergon }
8834db6bfe0SAlasdair G Kergon 
8841e302a92SJonathan Brassow static unsigned persistent_status(struct dm_exception_store *store,
885493df71cSJonathan Brassow 				  status_type_t status, char *result,
8861e302a92SJonathan Brassow 				  unsigned maxlen)
887493df71cSJonathan Brassow {
8881e302a92SJonathan Brassow 	unsigned sz = 0;
8891e302a92SJonathan Brassow 
8901e302a92SJonathan Brassow 	switch (status) {
8911e302a92SJonathan Brassow 	case STATUSTYPE_INFO:
8921e302a92SJonathan Brassow 		break;
8931e302a92SJonathan Brassow 	case STATUSTYPE_TABLE:
894fc56f6fbSMike Snitzer 		DMEMIT(" P %llu", (unsigned long long)store->chunk_size);
8951e302a92SJonathan Brassow 	}
896493df71cSJonathan Brassow 
897493df71cSJonathan Brassow 	return sz;
898493df71cSJonathan Brassow }
899493df71cSJonathan Brassow 
900493df71cSJonathan Brassow static struct dm_exception_store_type _persistent_type = {
901493df71cSJonathan Brassow 	.name = "persistent",
902493df71cSJonathan Brassow 	.module = THIS_MODULE,
903493df71cSJonathan Brassow 	.ctr = persistent_ctr,
904493df71cSJonathan Brassow 	.dtr = persistent_dtr,
905493df71cSJonathan Brassow 	.read_metadata = persistent_read_metadata,
906493df71cSJonathan Brassow 	.prepare_exception = persistent_prepare_exception,
907493df71cSJonathan Brassow 	.commit_exception = persistent_commit_exception,
9084454a621SMikulas Patocka 	.prepare_merge = persistent_prepare_merge,
9094454a621SMikulas Patocka 	.commit_merge = persistent_commit_merge,
910493df71cSJonathan Brassow 	.drop_snapshot = persistent_drop_snapshot,
911985903bbSMike Snitzer 	.usage = persistent_usage,
912493df71cSJonathan Brassow 	.status = persistent_status,
913493df71cSJonathan Brassow };
914493df71cSJonathan Brassow 
915493df71cSJonathan Brassow static struct dm_exception_store_type _persistent_compat_type = {
916493df71cSJonathan Brassow 	.name = "P",
917493df71cSJonathan Brassow 	.module = THIS_MODULE,
918493df71cSJonathan Brassow 	.ctr = persistent_ctr,
919493df71cSJonathan Brassow 	.dtr = persistent_dtr,
920493df71cSJonathan Brassow 	.read_metadata = persistent_read_metadata,
921493df71cSJonathan Brassow 	.prepare_exception = persistent_prepare_exception,
922493df71cSJonathan Brassow 	.commit_exception = persistent_commit_exception,
9234454a621SMikulas Patocka 	.prepare_merge = persistent_prepare_merge,
9244454a621SMikulas Patocka 	.commit_merge = persistent_commit_merge,
925493df71cSJonathan Brassow 	.drop_snapshot = persistent_drop_snapshot,
926985903bbSMike Snitzer 	.usage = persistent_usage,
927493df71cSJonathan Brassow 	.status = persistent_status,
928493df71cSJonathan Brassow };
929493df71cSJonathan Brassow 
9304db6bfe0SAlasdair G Kergon int dm_persistent_snapshot_init(void)
9314db6bfe0SAlasdair G Kergon {
932493df71cSJonathan Brassow 	int r;
933493df71cSJonathan Brassow 
934493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_persistent_type);
935493df71cSJonathan Brassow 	if (r) {
936493df71cSJonathan Brassow 		DMERR("Unable to register persistent exception store type");
937493df71cSJonathan Brassow 		return r;
938493df71cSJonathan Brassow 	}
939493df71cSJonathan Brassow 
940493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_persistent_compat_type);
941493df71cSJonathan Brassow 	if (r) {
942493df71cSJonathan Brassow 		DMERR("Unable to register old-style persistent exception "
943493df71cSJonathan Brassow 		      "store type");
944493df71cSJonathan Brassow 		dm_exception_store_type_unregister(&_persistent_type);
945493df71cSJonathan Brassow 		return r;
946493df71cSJonathan Brassow 	}
947493df71cSJonathan Brassow 
948493df71cSJonathan Brassow 	return r;
9494db6bfe0SAlasdair G Kergon }
9504db6bfe0SAlasdair G Kergon 
9514db6bfe0SAlasdair G Kergon void dm_persistent_snapshot_exit(void)
9524db6bfe0SAlasdair G Kergon {
953493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_persistent_type);
954493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_persistent_compat_type);
9554db6bfe0SAlasdair G Kergon }
956