13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
24db6bfe0SAlasdair G Kergon /*
34db6bfe0SAlasdair G Kergon  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
44db6bfe0SAlasdair G Kergon  * Copyright (C) 2006-2008 Red Hat GmbH
54db6bfe0SAlasdair G Kergon  *
64db6bfe0SAlasdair G Kergon  * This file is released under the GPL.
74db6bfe0SAlasdair G Kergon  */
84db6bfe0SAlasdair G Kergon 
94db6bfe0SAlasdair G Kergon #include "dm-exception-store.h"
104db6bfe0SAlasdair G Kergon 
11b0d3cc01SMike Snitzer #include <linux/ctype.h>
124db6bfe0SAlasdair G Kergon #include <linux/mm.h>
134db6bfe0SAlasdair G Kergon #include <linux/pagemap.h>
144db6bfe0SAlasdair G Kergon #include <linux/vmalloc.h>
15daaa5f7cSPaul Gortmaker #include <linux/export.h>
164db6bfe0SAlasdair G Kergon #include <linux/slab.h>
174db6bfe0SAlasdair G Kergon #include <linux/dm-io.h>
18afa53df8SMikulas Patocka #include <linux/dm-bufio.h>
194db6bfe0SAlasdair G Kergon 
204db6bfe0SAlasdair G Kergon #define DM_MSG_PREFIX "persistent snapshot"
21ad6bf88aSMikulas Patocka #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32U	/* 16KB */
224db6bfe0SAlasdair G Kergon 
2355b082e6SMikulas Patocka #define DM_PREFETCH_CHUNKS		12
2455b082e6SMikulas Patocka 
25a4a82ce3SHeinz Mauelshagen /*
26a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
274db6bfe0SAlasdair G Kergon  * Persistent snapshots, by persistent we mean that the snapshot
284db6bfe0SAlasdair G Kergon  * will survive a reboot.
29a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
30a4a82ce3SHeinz Mauelshagen  */
314db6bfe0SAlasdair G Kergon 
324db6bfe0SAlasdair G Kergon /*
334db6bfe0SAlasdair G Kergon  * We need to store a record of which parts of the origin have
344db6bfe0SAlasdair G Kergon  * been copied to the snapshot device.  The snapshot code
354db6bfe0SAlasdair G Kergon  * requires that we copy exception chunks to chunk aligned areas
364db6bfe0SAlasdair G Kergon  * of the COW store.  It makes sense therefore, to store the
374db6bfe0SAlasdair G Kergon  * metadata in chunk size blocks.
384db6bfe0SAlasdair G Kergon  *
394db6bfe0SAlasdair G Kergon  * There is no backward or forward compatibility implemented,
404db6bfe0SAlasdair G Kergon  * snapshots with different disk versions than the kernel will
414db6bfe0SAlasdair G Kergon  * not be usable.  It is expected that "lvcreate" will blank out
424db6bfe0SAlasdair G Kergon  * the start of a fresh COW device before calling the snapshot
434db6bfe0SAlasdair G Kergon  * constructor.
444db6bfe0SAlasdair G Kergon  *
454db6bfe0SAlasdair G Kergon  * The first chunk of the COW device just contains the header.
464db6bfe0SAlasdair G Kergon  * After this there is a chunk filled with exception metadata,
474db6bfe0SAlasdair G Kergon  * followed by as many exception chunks as can fit in the
484db6bfe0SAlasdair G Kergon  * metadata areas.
494db6bfe0SAlasdair G Kergon  *
504db6bfe0SAlasdair G Kergon  * All on disk structures are in little-endian format.  The end
514db6bfe0SAlasdair G Kergon  * of the exceptions info is indicated by an exception with a
524db6bfe0SAlasdair G Kergon  * new_chunk of 0, which is invalid since it would point to the
534db6bfe0SAlasdair G Kergon  * header chunk.
544db6bfe0SAlasdair G Kergon  */
554db6bfe0SAlasdair G Kergon 
564db6bfe0SAlasdair G Kergon /*
574db6bfe0SAlasdair G Kergon  * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
584db6bfe0SAlasdair G Kergon  */
594db6bfe0SAlasdair G Kergon #define SNAP_MAGIC 0x70416e53
604db6bfe0SAlasdair G Kergon 
614db6bfe0SAlasdair G Kergon /*
624db6bfe0SAlasdair G Kergon  * The on-disk version of the metadata.
634db6bfe0SAlasdair G Kergon  */
644db6bfe0SAlasdair G Kergon #define SNAPSHOT_DISK_VERSION 1
654db6bfe0SAlasdair G Kergon 
664454a621SMikulas Patocka #define NUM_SNAPSHOT_HDR_CHUNKS 1
674454a621SMikulas Patocka 
684db6bfe0SAlasdair G Kergon struct disk_header {
69283a8328SAlasdair G Kergon 	__le32 magic;
704db6bfe0SAlasdair G Kergon 
714db6bfe0SAlasdair G Kergon 	/*
724db6bfe0SAlasdair G Kergon 	 * Is this snapshot valid.  There is no way of recovering
734db6bfe0SAlasdair G Kergon 	 * an invalid snapshot.
744db6bfe0SAlasdair G Kergon 	 */
75283a8328SAlasdair G Kergon 	__le32 valid;
764db6bfe0SAlasdair G Kergon 
774db6bfe0SAlasdair G Kergon 	/*
784db6bfe0SAlasdair G Kergon 	 * Simple, incrementing version. no backward
794db6bfe0SAlasdair G Kergon 	 * compatibility.
804db6bfe0SAlasdair G Kergon 	 */
81283a8328SAlasdair G Kergon 	__le32 version;
824db6bfe0SAlasdair G Kergon 
834db6bfe0SAlasdair G Kergon 	/* In sectors */
84283a8328SAlasdair G Kergon 	__le32 chunk_size;
85283a8328SAlasdair G Kergon } __packed;
864db6bfe0SAlasdair G Kergon 
874db6bfe0SAlasdair G Kergon struct disk_exception {
88283a8328SAlasdair G Kergon 	__le64 old_chunk;
89283a8328SAlasdair G Kergon 	__le64 new_chunk;
90283a8328SAlasdair G Kergon } __packed;
91283a8328SAlasdair G Kergon 
92283a8328SAlasdair G Kergon struct core_exception {
934db6bfe0SAlasdair G Kergon 	uint64_t old_chunk;
944db6bfe0SAlasdair G Kergon 	uint64_t new_chunk;
954db6bfe0SAlasdair G Kergon };
964db6bfe0SAlasdair G Kergon 
974db6bfe0SAlasdair G Kergon struct commit_callback {
9802f10ba1SHeinz Mauelshagen 	void (*callback)(void *ref, int success);
994db6bfe0SAlasdair G Kergon 	void *context;
1004db6bfe0SAlasdair G Kergon };
1014db6bfe0SAlasdair G Kergon 
1024db6bfe0SAlasdair G Kergon /*
1034db6bfe0SAlasdair G Kergon  * The top level structure for a persistent exception store.
1044db6bfe0SAlasdair G Kergon  */
1054db6bfe0SAlasdair G Kergon struct pstore {
10671fab00aSJonathan Brassow 	struct dm_exception_store *store;
1074db6bfe0SAlasdair G Kergon 	int version;
1084db6bfe0SAlasdair G Kergon 	int valid;
1094db6bfe0SAlasdair G Kergon 	uint32_t exceptions_per_area;
1104db6bfe0SAlasdair G Kergon 
1114db6bfe0SAlasdair G Kergon 	/*
1124db6bfe0SAlasdair G Kergon 	 * Now that we have an asynchronous kcopyd there is no
1134db6bfe0SAlasdair G Kergon 	 * need for large chunk sizes, so it wont hurt to have a
1144db6bfe0SAlasdair G Kergon 	 * whole chunks worth of metadata in memory at once.
1154db6bfe0SAlasdair G Kergon 	 */
1164db6bfe0SAlasdair G Kergon 	void *area;
1174db6bfe0SAlasdair G Kergon 
1184db6bfe0SAlasdair G Kergon 	/*
1194db6bfe0SAlasdair G Kergon 	 * An area of zeros used to clear the next area.
1204db6bfe0SAlasdair G Kergon 	 */
1214db6bfe0SAlasdair G Kergon 	void *zero_area;
1224db6bfe0SAlasdair G Kergon 
1234db6bfe0SAlasdair G Kergon 	/*
12461578dcdSMikulas Patocka 	 * An area used for header. The header can be written
12561578dcdSMikulas Patocka 	 * concurrently with metadata (when invalidating the snapshot),
12661578dcdSMikulas Patocka 	 * so it needs a separate buffer.
12761578dcdSMikulas Patocka 	 */
12861578dcdSMikulas Patocka 	void *header_area;
12961578dcdSMikulas Patocka 
13061578dcdSMikulas Patocka 	/*
1314db6bfe0SAlasdair G Kergon 	 * Used to keep track of which metadata area the data in
1324db6bfe0SAlasdair G Kergon 	 * 'chunk' refers to.
1334db6bfe0SAlasdair G Kergon 	 */
1344db6bfe0SAlasdair G Kergon 	chunk_t current_area;
1354db6bfe0SAlasdair G Kergon 
1364db6bfe0SAlasdair G Kergon 	/*
1374db6bfe0SAlasdair G Kergon 	 * The next free chunk for an exception.
1384454a621SMikulas Patocka 	 *
1394454a621SMikulas Patocka 	 * When creating exceptions, all the chunks here and above are
1404454a621SMikulas Patocka 	 * free.  It holds the next chunk to be allocated.  On rare
1414454a621SMikulas Patocka 	 * occasions (e.g. after a system crash) holes can be left in
1424454a621SMikulas Patocka 	 * the exception store because chunks can be committed out of
1434454a621SMikulas Patocka 	 * order.
1444454a621SMikulas Patocka 	 *
1454454a621SMikulas Patocka 	 * When merging exceptions, it does not necessarily mean all the
1464454a621SMikulas Patocka 	 * chunks here and above are free.  It holds the value it would
1474454a621SMikulas Patocka 	 * have held if all chunks had been committed in order of
1484454a621SMikulas Patocka 	 * allocation.  Consequently the value may occasionally be
1494454a621SMikulas Patocka 	 * slightly too low, but since it's only used for 'status' and
1504454a621SMikulas Patocka 	 * it can never reach its minimum value too early this doesn't
1514454a621SMikulas Patocka 	 * matter.
1524db6bfe0SAlasdair G Kergon 	 */
1534454a621SMikulas Patocka 
1544db6bfe0SAlasdair G Kergon 	chunk_t next_free;
1554db6bfe0SAlasdair G Kergon 
1564db6bfe0SAlasdair G Kergon 	/*
1574db6bfe0SAlasdair G Kergon 	 * The index of next free exception in the current
1584db6bfe0SAlasdair G Kergon 	 * metadata area.
1594db6bfe0SAlasdair G Kergon 	 */
1604db6bfe0SAlasdair G Kergon 	uint32_t current_committed;
1614db6bfe0SAlasdair G Kergon 
1624db6bfe0SAlasdair G Kergon 	atomic_t pending_count;
1634db6bfe0SAlasdair G Kergon 	uint32_t callback_count;
1644db6bfe0SAlasdair G Kergon 	struct commit_callback *callbacks;
1654db6bfe0SAlasdair G Kergon 	struct dm_io_client *io_client;
1664db6bfe0SAlasdair G Kergon 
1674db6bfe0SAlasdair G Kergon 	struct workqueue_struct *metadata_wq;
1684db6bfe0SAlasdair G Kergon };
1694db6bfe0SAlasdair G Kergon 
alloc_area(struct pstore * ps)1704db6bfe0SAlasdair G Kergon static int alloc_area(struct pstore *ps)
1714db6bfe0SAlasdair G Kergon {
1724db6bfe0SAlasdair G Kergon 	int r = -ENOMEM;
1734db6bfe0SAlasdair G Kergon 	size_t len;
1744db6bfe0SAlasdair G Kergon 
17571fab00aSJonathan Brassow 	len = ps->store->chunk_size << SECTOR_SHIFT;
1764db6bfe0SAlasdair G Kergon 
1774db6bfe0SAlasdair G Kergon 	/*
1784db6bfe0SAlasdair G Kergon 	 * Allocate the chunk_size block of memory that will hold
1794db6bfe0SAlasdair G Kergon 	 * a single metadata area.
1804db6bfe0SAlasdair G Kergon 	 */
1814db6bfe0SAlasdair G Kergon 	ps->area = vmalloc(len);
1824db6bfe0SAlasdair G Kergon 	if (!ps->area)
18361578dcdSMikulas Patocka 		goto err_area;
1844db6bfe0SAlasdair G Kergon 
185e29e65aaSJoe Perches 	ps->zero_area = vzalloc(len);
18661578dcdSMikulas Patocka 	if (!ps->zero_area)
18761578dcdSMikulas Patocka 		goto err_zero_area;
1884db6bfe0SAlasdair G Kergon 
18961578dcdSMikulas Patocka 	ps->header_area = vmalloc(len);
19061578dcdSMikulas Patocka 	if (!ps->header_area)
19161578dcdSMikulas Patocka 		goto err_header_area;
19261578dcdSMikulas Patocka 
1934db6bfe0SAlasdair G Kergon 	return 0;
19461578dcdSMikulas Patocka 
19561578dcdSMikulas Patocka err_header_area:
19661578dcdSMikulas Patocka 	vfree(ps->zero_area);
19761578dcdSMikulas Patocka 
19861578dcdSMikulas Patocka err_zero_area:
19961578dcdSMikulas Patocka 	vfree(ps->area);
20061578dcdSMikulas Patocka 
20161578dcdSMikulas Patocka err_area:
20261578dcdSMikulas Patocka 	return r;
2034db6bfe0SAlasdair G Kergon }
2044db6bfe0SAlasdair G Kergon 
free_area(struct pstore * ps)2054db6bfe0SAlasdair G Kergon static void free_area(struct pstore *ps)
2064db6bfe0SAlasdair G Kergon {
2074db6bfe0SAlasdair G Kergon 	vfree(ps->area);
2084db6bfe0SAlasdair G Kergon 	ps->area = NULL;
2094db6bfe0SAlasdair G Kergon 	vfree(ps->zero_area);
2104db6bfe0SAlasdair G Kergon 	ps->zero_area = NULL;
21161578dcdSMikulas Patocka 	vfree(ps->header_area);
21261578dcdSMikulas Patocka 	ps->header_area = NULL;
2134db6bfe0SAlasdair G Kergon }
2144db6bfe0SAlasdair G Kergon 
2154db6bfe0SAlasdair G Kergon struct mdata_req {
2164db6bfe0SAlasdair G Kergon 	struct dm_io_region *where;
2174db6bfe0SAlasdair G Kergon 	struct dm_io_request *io_req;
2184db6bfe0SAlasdair G Kergon 	struct work_struct work;
2194db6bfe0SAlasdair G Kergon 	int result;
2204db6bfe0SAlasdair G Kergon };
2214db6bfe0SAlasdair G Kergon 
do_metadata(struct work_struct * work)2224db6bfe0SAlasdair G Kergon static void do_metadata(struct work_struct *work)
2234db6bfe0SAlasdair G Kergon {
2244db6bfe0SAlasdair G Kergon 	struct mdata_req *req = container_of(work, struct mdata_req, work);
2254db6bfe0SAlasdair G Kergon 
226*5cfcea64SHongyu Jin 	req->result = dm_io(req->io_req, 1, req->where, NULL, IOPRIO_DEFAULT);
2274db6bfe0SAlasdair G Kergon }
2284db6bfe0SAlasdair G Kergon 
2294db6bfe0SAlasdair G Kergon /*
2304db6bfe0SAlasdair G Kergon  * Read or write a chunk aligned and sized block of data from a device.
2314db6bfe0SAlasdair G Kergon  */
chunk_io(struct pstore * ps,void * area,chunk_t chunk,blk_opf_t opf,int metadata)2326b990139SBart Van Assche static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, blk_opf_t opf,
2336b990139SBart Van Assche 		    int metadata)
2344db6bfe0SAlasdair G Kergon {
2354db6bfe0SAlasdair G Kergon 	struct dm_io_region where = {
236fc56f6fbSMike Snitzer 		.bdev = dm_snap_cow(ps->store->snap)->bdev,
23771fab00aSJonathan Brassow 		.sector = ps->store->chunk_size * chunk,
23871fab00aSJonathan Brassow 		.count = ps->store->chunk_size,
2394db6bfe0SAlasdair G Kergon 	};
2404db6bfe0SAlasdair G Kergon 	struct dm_io_request io_req = {
2416b990139SBart Van Assche 		.bi_opf = opf,
2424db6bfe0SAlasdair G Kergon 		.mem.type = DM_IO_VMA,
24302d2fd31SMikulas Patocka 		.mem.ptr.vma = area,
2444db6bfe0SAlasdair G Kergon 		.client = ps->io_client,
2454db6bfe0SAlasdair G Kergon 		.notify.fn = NULL,
2464db6bfe0SAlasdair G Kergon 	};
2474db6bfe0SAlasdair G Kergon 	struct mdata_req req;
2484db6bfe0SAlasdair G Kergon 
2494db6bfe0SAlasdair G Kergon 	if (!metadata)
250*5cfcea64SHongyu Jin 		return dm_io(&io_req, 1, &where, NULL, IOPRIO_DEFAULT);
2514db6bfe0SAlasdair G Kergon 
2524db6bfe0SAlasdair G Kergon 	req.where = &where;
2534db6bfe0SAlasdair G Kergon 	req.io_req = &io_req;
2544db6bfe0SAlasdair G Kergon 
2554db6bfe0SAlasdair G Kergon 	/*
2564db6bfe0SAlasdair G Kergon 	 * Issue the synchronous I/O from a different thread
257ed00aabdSChristoph Hellwig 	 * to avoid submit_bio_noacct recursion.
2584db6bfe0SAlasdair G Kergon 	 */
259ca1cab37SAndrew Morton 	INIT_WORK_ONSTACK(&req.work, do_metadata);
2604db6bfe0SAlasdair G Kergon 	queue_work(ps->metadata_wq, &req.work);
2615ea330a7SMikulas Patocka 	flush_workqueue(ps->metadata_wq);
262c1a64160SChuansheng Liu 	destroy_work_on_stack(&req.work);
2634db6bfe0SAlasdair G Kergon 
2644db6bfe0SAlasdair G Kergon 	return req.result;
2654db6bfe0SAlasdair G Kergon }
2664db6bfe0SAlasdair G Kergon 
2674db6bfe0SAlasdair G Kergon /*
2684db6bfe0SAlasdair G Kergon  * Convert a metadata area index to a chunk index.
2694db6bfe0SAlasdair G Kergon  */
area_location(struct pstore * ps,chunk_t area)2704db6bfe0SAlasdair G Kergon static chunk_t area_location(struct pstore *ps, chunk_t area)
2714db6bfe0SAlasdair G Kergon {
27287c961cbSTomohiro Kusumi 	return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area);
2734db6bfe0SAlasdair G Kergon }
2744db6bfe0SAlasdair G Kergon 
skip_metadata(struct pstore * ps)275e9c6a182SMikulas Patocka static void skip_metadata(struct pstore *ps)
276e9c6a182SMikulas Patocka {
277e9c6a182SMikulas Patocka 	uint32_t stride = ps->exceptions_per_area + 1;
278e9c6a182SMikulas Patocka 	chunk_t next_free = ps->next_free;
2790ef0b471SHeinz Mauelshagen 
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  */
area_io(struct pstore * ps,blk_opf_t opf)2886b990139SBart Van Assche static int area_io(struct pstore *ps, blk_opf_t opf)
2894db6bfe0SAlasdair G Kergon {
2907d837c0dSQinglang Miao 	chunk_t chunk = area_location(ps, ps->current_area);
2914db6bfe0SAlasdair G Kergon 
2926b990139SBart Van Assche 	return chunk_io(ps, ps->area, chunk, opf, 0);
2934db6bfe0SAlasdair G Kergon }
2944db6bfe0SAlasdair G Kergon 
zero_memory_area(struct pstore * ps)2954db6bfe0SAlasdair G Kergon static void zero_memory_area(struct pstore *ps)
2964db6bfe0SAlasdair G Kergon {
29771fab00aSJonathan Brassow 	memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
2984db6bfe0SAlasdair G Kergon }
2994db6bfe0SAlasdair G Kergon 
zero_disk_area(struct pstore * ps,chunk_t area)3004db6bfe0SAlasdair G Kergon static int zero_disk_area(struct pstore *ps, chunk_t area)
3014db6bfe0SAlasdair G Kergon {
302e6047149SMike Christie 	return chunk_io(ps, ps->zero_area, area_location(ps, area),
3036b990139SBart Van Assche 			REQ_OP_WRITE, 0);
3044db6bfe0SAlasdair G Kergon }
3054db6bfe0SAlasdair G Kergon 
read_header(struct pstore * ps,int * new_snapshot)3064db6bfe0SAlasdair G Kergon static int read_header(struct pstore *ps, int *new_snapshot)
3074db6bfe0SAlasdair G Kergon {
3084db6bfe0SAlasdair G Kergon 	int r;
3094db6bfe0SAlasdair G Kergon 	struct disk_header *dh;
31086a3238cSHeinz Mauelshagen 	unsigned int chunk_size;
3114db6bfe0SAlasdair G Kergon 	int chunk_size_supplied = 1;
312ae0b7448SMikulas Patocka 	char *chunk_err;
3134db6bfe0SAlasdair G Kergon 
3144db6bfe0SAlasdair G Kergon 	/*
315df96eee6SMikulas Patocka 	 * Use default chunk size (or logical_block_size, if larger)
316df96eee6SMikulas Patocka 	 * if none supplied
3174db6bfe0SAlasdair G Kergon 	 */
31871fab00aSJonathan Brassow 	if (!ps->store->chunk_size) {
31971fab00aSJonathan Brassow 		ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
320fc56f6fbSMike Snitzer 		    bdev_logical_block_size(dm_snap_cow(ps->store->snap)->
321fc56f6fbSMike Snitzer 					    bdev) >> 9);
32271fab00aSJonathan Brassow 		ps->store->chunk_mask = ps->store->chunk_size - 1;
323a3d939aeSMikulas Patocka 		ps->store->chunk_shift = __ffs(ps->store->chunk_size);
3244db6bfe0SAlasdair G Kergon 		chunk_size_supplied = 0;
3254db6bfe0SAlasdair G Kergon 	}
3264db6bfe0SAlasdair G Kergon 
327bda8efecSMikulas Patocka 	ps->io_client = dm_io_client_create();
3284db6bfe0SAlasdair G Kergon 	if (IS_ERR(ps->io_client))
3294db6bfe0SAlasdair G Kergon 		return PTR_ERR(ps->io_client);
3304db6bfe0SAlasdair G Kergon 
3314db6bfe0SAlasdair G Kergon 	r = alloc_area(ps);
3324db6bfe0SAlasdair G Kergon 	if (r)
3334db6bfe0SAlasdair G Kergon 		return r;
3344db6bfe0SAlasdair G Kergon 
3356b990139SBart Van Assche 	r = chunk_io(ps, ps->header_area, 0, REQ_OP_READ, 1);
3364db6bfe0SAlasdair G Kergon 	if (r)
3374db6bfe0SAlasdair G Kergon 		goto bad;
3384db6bfe0SAlasdair G Kergon 
33961578dcdSMikulas Patocka 	dh = ps->header_area;
3404db6bfe0SAlasdair G Kergon 
3414db6bfe0SAlasdair G Kergon 	if (le32_to_cpu(dh->magic) == 0) {
3424db6bfe0SAlasdair G Kergon 		*new_snapshot = 1;
3434db6bfe0SAlasdair G Kergon 		return 0;
3444db6bfe0SAlasdair G Kergon 	}
3454db6bfe0SAlasdair G Kergon 
3464db6bfe0SAlasdair G Kergon 	if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
3474db6bfe0SAlasdair G Kergon 		DMWARN("Invalid or corrupt snapshot");
3484db6bfe0SAlasdair G Kergon 		r = -ENXIO;
3494db6bfe0SAlasdair G Kergon 		goto bad;
3504db6bfe0SAlasdair G Kergon 	}
3514db6bfe0SAlasdair G Kergon 
3524db6bfe0SAlasdair G Kergon 	*new_snapshot = 0;
3534db6bfe0SAlasdair G Kergon 	ps->valid = le32_to_cpu(dh->valid);
3544db6bfe0SAlasdair G Kergon 	ps->version = le32_to_cpu(dh->version);
3554db6bfe0SAlasdair G Kergon 	chunk_size = le32_to_cpu(dh->chunk_size);
3564db6bfe0SAlasdair G Kergon 
357ae0b7448SMikulas Patocka 	if (ps->store->chunk_size == chunk_size)
3584db6bfe0SAlasdair G Kergon 		return 0;
3594db6bfe0SAlasdair G Kergon 
360ae0b7448SMikulas Patocka 	if (chunk_size_supplied)
3612e84fecfSHeinz Mauelshagen 		DMWARN("chunk size %u in device metadata overrides table chunk size of %u.",
362df96eee6SMikulas Patocka 		       chunk_size, ps->store->chunk_size);
3634db6bfe0SAlasdair G Kergon 
3644db6bfe0SAlasdair G Kergon 	/* We had a bogus chunk_size. Fix stuff up. */
3654db6bfe0SAlasdair G Kergon 	free_area(ps);
3664db6bfe0SAlasdair G Kergon 
367ae0b7448SMikulas Patocka 	r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
368ae0b7448SMikulas Patocka 					      &chunk_err);
369ae0b7448SMikulas Patocka 	if (r) {
370df96eee6SMikulas Patocka 		DMERR("invalid on-disk chunk size %u: %s.",
371df96eee6SMikulas Patocka 		      chunk_size, chunk_err);
372ae0b7448SMikulas Patocka 		return r;
373ae0b7448SMikulas Patocka 	}
3744db6bfe0SAlasdair G Kergon 
3754db6bfe0SAlasdair G Kergon 	r = alloc_area(ps);
3764db6bfe0SAlasdair G Kergon 	return r;
3774db6bfe0SAlasdair G Kergon 
3784db6bfe0SAlasdair G Kergon bad:
3794db6bfe0SAlasdair G Kergon 	free_area(ps);
3804db6bfe0SAlasdair G Kergon 	return r;
3814db6bfe0SAlasdair G Kergon }
3824db6bfe0SAlasdair G Kergon 
write_header(struct pstore * ps)3834db6bfe0SAlasdair G Kergon static int write_header(struct pstore *ps)
3844db6bfe0SAlasdair G Kergon {
3854db6bfe0SAlasdair G Kergon 	struct disk_header *dh;
3864db6bfe0SAlasdair G Kergon 
38761578dcdSMikulas Patocka 	memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
3884db6bfe0SAlasdair G Kergon 
38961578dcdSMikulas Patocka 	dh = ps->header_area;
3904db6bfe0SAlasdair G Kergon 	dh->magic = cpu_to_le32(SNAP_MAGIC);
3914db6bfe0SAlasdair G Kergon 	dh->valid = cpu_to_le32(ps->valid);
3924db6bfe0SAlasdair G Kergon 	dh->version = cpu_to_le32(ps->version);
39371fab00aSJonathan Brassow 	dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
3944db6bfe0SAlasdair G Kergon 
3956b990139SBart Van Assche 	return chunk_io(ps, ps->header_area, 0, REQ_OP_WRITE, 1);
3964db6bfe0SAlasdair G Kergon }
3974db6bfe0SAlasdair G Kergon 
3984db6bfe0SAlasdair G Kergon /*
3994db6bfe0SAlasdair G Kergon  * Access functions for the disk exceptions, these do the endian conversions.
4004db6bfe0SAlasdair G Kergon  */
get_exception(struct pstore * ps,void * ps_area,uint32_t index)4012cadabd5SMikulas Patocka static struct disk_exception *get_exception(struct pstore *ps, void *ps_area,
4022cadabd5SMikulas Patocka 					    uint32_t index)
4034db6bfe0SAlasdair G Kergon {
4044db6bfe0SAlasdair G Kergon 	BUG_ON(index >= ps->exceptions_per_area);
4054db6bfe0SAlasdair G Kergon 
4062cadabd5SMikulas Patocka 	return ((struct disk_exception *) ps_area) + index;
4074db6bfe0SAlasdair G Kergon }
4084db6bfe0SAlasdair G Kergon 
read_exception(struct pstore * ps,void * ps_area,uint32_t index,struct core_exception * result)4092cadabd5SMikulas Patocka static void read_exception(struct pstore *ps, void *ps_area,
410283a8328SAlasdair G Kergon 			   uint32_t index, struct core_exception *result)
4114db6bfe0SAlasdair G Kergon {
4122cadabd5SMikulas Patocka 	struct disk_exception *de = get_exception(ps, ps_area, index);
4134db6bfe0SAlasdair G Kergon 
4144db6bfe0SAlasdair G Kergon 	/* copy it */
415283a8328SAlasdair G Kergon 	result->old_chunk = le64_to_cpu(de->old_chunk);
416283a8328SAlasdair G Kergon 	result->new_chunk = le64_to_cpu(de->new_chunk);
4174db6bfe0SAlasdair G Kergon }
4184db6bfe0SAlasdair G Kergon 
write_exception(struct pstore * ps,uint32_t index,struct core_exception * e)4194db6bfe0SAlasdair G Kergon static void write_exception(struct pstore *ps,
420283a8328SAlasdair G Kergon 			    uint32_t index, struct core_exception *e)
4214db6bfe0SAlasdair G Kergon {
4222cadabd5SMikulas Patocka 	struct disk_exception *de = get_exception(ps, ps->area, index);
4234db6bfe0SAlasdair G Kergon 
4244db6bfe0SAlasdair G Kergon 	/* copy it */
425283a8328SAlasdair G Kergon 	de->old_chunk = cpu_to_le64(e->old_chunk);
426283a8328SAlasdair G Kergon 	de->new_chunk = cpu_to_le64(e->new_chunk);
4274db6bfe0SAlasdair G Kergon }
4284db6bfe0SAlasdair G Kergon 
clear_exception(struct pstore * ps,uint32_t index)4294454a621SMikulas Patocka static void clear_exception(struct pstore *ps, uint32_t index)
4304454a621SMikulas Patocka {
4312cadabd5SMikulas Patocka 	struct disk_exception *de = get_exception(ps, ps->area, index);
4324454a621SMikulas Patocka 
4334454a621SMikulas Patocka 	/* clear it */
434283a8328SAlasdair G Kergon 	de->old_chunk = 0;
435283a8328SAlasdair G Kergon 	de->new_chunk = 0;
4364454a621SMikulas Patocka }
4374454a621SMikulas Patocka 
4384db6bfe0SAlasdair G Kergon /*
4394db6bfe0SAlasdair G Kergon  * Registers the exceptions that are present in the current area.
4404db6bfe0SAlasdair G Kergon  * 'full' is filled in to indicate if the area has been
4414db6bfe0SAlasdair G Kergon  * filled.
4424db6bfe0SAlasdair G Kergon  */
insert_exceptions(struct pstore * ps,void * ps_area,int (* callback)(void * callback_context,chunk_t old,chunk_t new),void * callback_context,int * full)4432cadabd5SMikulas Patocka static int insert_exceptions(struct pstore *ps, void *ps_area,
444a159c1acSJonathan Brassow 			     int (*callback)(void *callback_context,
445a159c1acSJonathan Brassow 					     chunk_t old, chunk_t new),
446a159c1acSJonathan Brassow 			     void *callback_context,
447a159c1acSJonathan Brassow 			     int *full)
4484db6bfe0SAlasdair G Kergon {
4494db6bfe0SAlasdair G Kergon 	int r;
4504db6bfe0SAlasdair G Kergon 	unsigned int i;
451283a8328SAlasdair G Kergon 	struct core_exception e;
4524db6bfe0SAlasdair G Kergon 
4534db6bfe0SAlasdair G Kergon 	/* presume the area is full */
4544db6bfe0SAlasdair G Kergon 	*full = 1;
4554db6bfe0SAlasdair G Kergon 
4564db6bfe0SAlasdair G Kergon 	for (i = 0; i < ps->exceptions_per_area; i++) {
4572cadabd5SMikulas Patocka 		read_exception(ps, ps_area, i, &e);
4584db6bfe0SAlasdair G Kergon 
4594db6bfe0SAlasdair G Kergon 		/*
4604db6bfe0SAlasdair G Kergon 		 * If the new_chunk is pointing at the start of
4614db6bfe0SAlasdair G Kergon 		 * the COW device, where the first metadata area
4624db6bfe0SAlasdair G Kergon 		 * is we know that we've hit the end of the
4634db6bfe0SAlasdair G Kergon 		 * exceptions.  Therefore the area is not full.
4644db6bfe0SAlasdair G Kergon 		 */
465283a8328SAlasdair G Kergon 		if (e.new_chunk == 0LL) {
4664db6bfe0SAlasdair G Kergon 			ps->current_committed = i;
4674db6bfe0SAlasdair G Kergon 			*full = 0;
4684db6bfe0SAlasdair G Kergon 			break;
4694db6bfe0SAlasdair G Kergon 		}
4704db6bfe0SAlasdair G Kergon 
4714db6bfe0SAlasdair G Kergon 		/*
4724db6bfe0SAlasdair G Kergon 		 * Keep track of the start of the free chunks.
4734db6bfe0SAlasdair G Kergon 		 */
474283a8328SAlasdair G Kergon 		if (ps->next_free <= e.new_chunk)
475283a8328SAlasdair G Kergon 			ps->next_free = e.new_chunk + 1;
4764db6bfe0SAlasdair G Kergon 
4774db6bfe0SAlasdair G Kergon 		/*
4784db6bfe0SAlasdair G Kergon 		 * Otherwise we add the exception to the snapshot.
4794db6bfe0SAlasdair G Kergon 		 */
480283a8328SAlasdair G Kergon 		r = callback(callback_context, e.old_chunk, e.new_chunk);
4814db6bfe0SAlasdair G Kergon 		if (r)
4824db6bfe0SAlasdair G Kergon 			return r;
4834db6bfe0SAlasdair G Kergon 	}
4844db6bfe0SAlasdair G Kergon 
4854db6bfe0SAlasdair G Kergon 	return 0;
4864db6bfe0SAlasdair G Kergon }
4874db6bfe0SAlasdair G Kergon 
read_exceptions(struct pstore * ps,int (* callback)(void * callback_context,chunk_t old,chunk_t new),void * callback_context)488a159c1acSJonathan Brassow static int read_exceptions(struct pstore *ps,
489a159c1acSJonathan Brassow 			   int (*callback)(void *callback_context, chunk_t old,
490a159c1acSJonathan Brassow 					   chunk_t new),
491a159c1acSJonathan Brassow 			   void *callback_context)
4924db6bfe0SAlasdair G Kergon {
4934db6bfe0SAlasdair G Kergon 	int r, full = 1;
49455494bf2SMikulas Patocka 	struct dm_bufio_client *client;
49555b082e6SMikulas Patocka 	chunk_t prefetch_area = 0;
49655494bf2SMikulas Patocka 
49755494bf2SMikulas Patocka 	client = dm_bufio_client_create(dm_snap_cow(ps->store->snap)->bdev,
49855494bf2SMikulas Patocka 					ps->store->chunk_size << SECTOR_SHIFT,
4990fcb100dSNathan Huckleberry 					1, 0, NULL, NULL, 0);
50055494bf2SMikulas Patocka 
50155494bf2SMikulas Patocka 	if (IS_ERR(client))
50255494bf2SMikulas Patocka 		return PTR_ERR(client);
5034db6bfe0SAlasdair G Kergon 
5044db6bfe0SAlasdair G Kergon 	/*
50555b082e6SMikulas Patocka 	 * Setup for one current buffer + desired readahead buffers.
50655b082e6SMikulas Patocka 	 */
50755b082e6SMikulas Patocka 	dm_bufio_set_minimum_buffers(client, 1 + DM_PREFETCH_CHUNKS);
50855b082e6SMikulas Patocka 
50955b082e6SMikulas Patocka 	/*
5104db6bfe0SAlasdair G Kergon 	 * Keeping reading chunks and inserting exceptions until
5114db6bfe0SAlasdair G Kergon 	 * we find a partially full area.
5124db6bfe0SAlasdair G Kergon 	 */
5134db6bfe0SAlasdair G Kergon 	for (ps->current_area = 0; full; ps->current_area++) {
51455494bf2SMikulas Patocka 		struct dm_buffer *bp;
51555494bf2SMikulas Patocka 		void *area;
51655b082e6SMikulas Patocka 		chunk_t chunk;
51755b082e6SMikulas Patocka 
51855b082e6SMikulas Patocka 		if (unlikely(prefetch_area < ps->current_area))
51955b082e6SMikulas Patocka 			prefetch_area = ps->current_area;
52055b082e6SMikulas Patocka 
52103b18887SHeinz Mauelshagen 		if (DM_PREFETCH_CHUNKS) {
52203b18887SHeinz Mauelshagen 			do {
52355b082e6SMikulas Patocka 				chunk_t pf_chunk = area_location(ps, prefetch_area);
5240ef0b471SHeinz Mauelshagen 
52555b082e6SMikulas Patocka 				if (unlikely(pf_chunk >= dm_bufio_get_device_size(client)))
52655b082e6SMikulas Patocka 					break;
52755b082e6SMikulas Patocka 				dm_bufio_prefetch(client, pf_chunk, 1);
52855b082e6SMikulas Patocka 				prefetch_area++;
52955b082e6SMikulas Patocka 				if (unlikely(!prefetch_area))
53055b082e6SMikulas Patocka 					break;
53155b082e6SMikulas Patocka 			} while (prefetch_area <= ps->current_area + DM_PREFETCH_CHUNKS);
53203b18887SHeinz Mauelshagen 		}
53355b082e6SMikulas Patocka 
53455b082e6SMikulas Patocka 		chunk = area_location(ps, ps->current_area);
5354db6bfe0SAlasdair G Kergon 
53655494bf2SMikulas Patocka 		area = dm_bufio_read(client, chunk, &bp);
537fc0a4461Sviresh kumar 		if (IS_ERR(area)) {
53855494bf2SMikulas Patocka 			r = PTR_ERR(area);
53955494bf2SMikulas Patocka 			goto ret_destroy_bufio;
54055494bf2SMikulas Patocka 		}
54155494bf2SMikulas Patocka 
54255494bf2SMikulas Patocka 		r = insert_exceptions(ps, area, callback, callback_context,
5432cadabd5SMikulas Patocka 				      &full);
54455494bf2SMikulas Patocka 
5452c945820SMikulas Patocka 		if (!full)
5462c945820SMikulas Patocka 			memcpy(ps->area, area, ps->store->chunk_size << SECTOR_SHIFT);
5472c945820SMikulas Patocka 
54855494bf2SMikulas Patocka 		dm_bufio_release(bp);
54955494bf2SMikulas Patocka 
55055494bf2SMikulas Patocka 		dm_bufio_forget(client, chunk);
55155494bf2SMikulas Patocka 
55255494bf2SMikulas Patocka 		if (unlikely(r))
55355494bf2SMikulas Patocka 			goto ret_destroy_bufio;
5544db6bfe0SAlasdair G Kergon 	}
5554db6bfe0SAlasdair G Kergon 
5564db6bfe0SAlasdair G Kergon 	ps->current_area--;
5574db6bfe0SAlasdair G Kergon 
558e9c6a182SMikulas Patocka 	skip_metadata(ps);
559e9c6a182SMikulas Patocka 
56055494bf2SMikulas Patocka 	r = 0;
56155494bf2SMikulas Patocka 
56255494bf2SMikulas Patocka ret_destroy_bufio:
56355494bf2SMikulas Patocka 	dm_bufio_client_destroy(client);
56455494bf2SMikulas Patocka 
56555494bf2SMikulas Patocka 	return r;
5664db6bfe0SAlasdair G Kergon }
5674db6bfe0SAlasdair G Kergon 
get_info(struct dm_exception_store * store)5684db6bfe0SAlasdair G Kergon static struct pstore *get_info(struct dm_exception_store *store)
5694db6bfe0SAlasdair G Kergon {
57026cb62a2SYu Zhe 	return store->context;
5714db6bfe0SAlasdair G Kergon }
5724db6bfe0SAlasdair G Kergon 
persistent_usage(struct dm_exception_store * store,sector_t * total_sectors,sector_t * sectors_allocated,sector_t * metadata_sectors)573985903bbSMike Snitzer static void persistent_usage(struct dm_exception_store *store,
574985903bbSMike Snitzer 			     sector_t *total_sectors,
575985903bbSMike Snitzer 			     sector_t *sectors_allocated,
576985903bbSMike Snitzer 			     sector_t *metadata_sectors)
5774db6bfe0SAlasdair G Kergon {
578985903bbSMike Snitzer 	struct pstore *ps = get_info(store);
579985903bbSMike Snitzer 
580985903bbSMike Snitzer 	*sectors_allocated = ps->next_free * store->chunk_size;
581fc56f6fbSMike Snitzer 	*total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
582985903bbSMike Snitzer 
583985903bbSMike Snitzer 	/*
584985903bbSMike Snitzer 	 * First chunk is the fixed header.
585985903bbSMike Snitzer 	 * Then there are (ps->current_area + 1) metadata chunks, each one
586985903bbSMike Snitzer 	 * separated from the next by ps->exceptions_per_area data chunks.
587985903bbSMike Snitzer 	 */
5884454a621SMikulas Patocka 	*metadata_sectors = (ps->current_area + 1 + NUM_SNAPSHOT_HDR_CHUNKS) *
5894454a621SMikulas Patocka 			    store->chunk_size;
5904db6bfe0SAlasdair G Kergon }
5914db6bfe0SAlasdair G Kergon 
persistent_dtr(struct dm_exception_store * store)592493df71cSJonathan Brassow static void persistent_dtr(struct dm_exception_store *store)
5934db6bfe0SAlasdair G Kergon {
5944db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
5954db6bfe0SAlasdair G Kergon 
5964db6bfe0SAlasdair G Kergon 	destroy_workqueue(ps->metadata_wq);
597a32079ceSJonathan Brassow 
598a32079ceSJonathan Brassow 	/* Created in read_header */
599a32079ceSJonathan Brassow 	if (ps->io_client)
6004db6bfe0SAlasdair G Kergon 		dm_io_client_destroy(ps->io_client);
6014db6bfe0SAlasdair G Kergon 	free_area(ps);
602a32079ceSJonathan Brassow 
603a32079ceSJonathan Brassow 	/* Allocated in persistent_read_metadata */
6047a35693aSMatthew Wilcox (Oracle) 	kvfree(ps->callbacks);
605a32079ceSJonathan Brassow 
6064db6bfe0SAlasdair G Kergon 	kfree(ps);
6074db6bfe0SAlasdair G Kergon }
6084db6bfe0SAlasdair G Kergon 
persistent_read_metadata(struct dm_exception_store * store,int (* callback)(void * callback_context,chunk_t old,chunk_t new),void * callback_context)609a159c1acSJonathan Brassow static int persistent_read_metadata(struct dm_exception_store *store,
610a159c1acSJonathan Brassow 				    int (*callback)(void *callback_context,
611a159c1acSJonathan Brassow 						    chunk_t old, chunk_t new),
612a159c1acSJonathan Brassow 				    void *callback_context)
6134db6bfe0SAlasdair G Kergon {
6143f649ab7SKees Cook 	int r, new_snapshot;
6154db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
6164db6bfe0SAlasdair G Kergon 
6174db6bfe0SAlasdair G Kergon 	/*
6184db6bfe0SAlasdair G Kergon 	 * Read the snapshot header.
6194db6bfe0SAlasdair G Kergon 	 */
6204db6bfe0SAlasdair G Kergon 	r = read_header(ps, &new_snapshot);
6214db6bfe0SAlasdair G Kergon 	if (r)
6224db6bfe0SAlasdair G Kergon 		return r;
6234db6bfe0SAlasdair G Kergon 
6244db6bfe0SAlasdair G Kergon 	/*
6254db6bfe0SAlasdair G Kergon 	 * Now we know correct chunk_size, complete the initialisation.
6264db6bfe0SAlasdair G Kergon 	 */
62771fab00aSJonathan Brassow 	ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
62871fab00aSJonathan Brassow 				  sizeof(struct disk_exception);
6297a35693aSMatthew Wilcox (Oracle) 	ps->callbacks = kvcalloc(ps->exceptions_per_area,
6307a35693aSMatthew Wilcox (Oracle) 				 sizeof(*ps->callbacks), GFP_KERNEL);
6314db6bfe0SAlasdair G Kergon 	if (!ps->callbacks)
6324db6bfe0SAlasdair G Kergon 		return -ENOMEM;
6334db6bfe0SAlasdair G Kergon 
6344db6bfe0SAlasdair G Kergon 	/*
6354db6bfe0SAlasdair G Kergon 	 * Do we need to setup a new snapshot ?
6364db6bfe0SAlasdair G Kergon 	 */
6374db6bfe0SAlasdair G Kergon 	if (new_snapshot) {
6384db6bfe0SAlasdair G Kergon 		r = write_header(ps);
6394db6bfe0SAlasdair G Kergon 		if (r) {
6404db6bfe0SAlasdair G Kergon 			DMWARN("write_header failed");
6414db6bfe0SAlasdair G Kergon 			return r;
6424db6bfe0SAlasdair G Kergon 		}
6434db6bfe0SAlasdair G Kergon 
6444db6bfe0SAlasdair G Kergon 		ps->current_area = 0;
6454db6bfe0SAlasdair G Kergon 		zero_memory_area(ps);
6464db6bfe0SAlasdair G Kergon 		r = zero_disk_area(ps, 0);
647f5acc834SJon Brassow 		if (r)
6484db6bfe0SAlasdair G Kergon 			DMWARN("zero_disk_area(0) failed");
6494db6bfe0SAlasdair G Kergon 		return r;
6504db6bfe0SAlasdair G Kergon 	}
6514db6bfe0SAlasdair G Kergon 	/*
6524db6bfe0SAlasdair G Kergon 	 * Sanity checks.
6534db6bfe0SAlasdair G Kergon 	 */
6544db6bfe0SAlasdair G Kergon 	if (ps->version != SNAPSHOT_DISK_VERSION) {
6554db6bfe0SAlasdair G Kergon 		DMWARN("unable to handle snapshot disk version %d",
6564db6bfe0SAlasdair G Kergon 		       ps->version);
6574db6bfe0SAlasdair G Kergon 		return -EINVAL;
6584db6bfe0SAlasdair G Kergon 	}
6594db6bfe0SAlasdair G Kergon 
6604db6bfe0SAlasdair G Kergon 	/*
6614db6bfe0SAlasdair G Kergon 	 * Metadata are valid, but snapshot is invalidated
6624db6bfe0SAlasdair G Kergon 	 */
6634db6bfe0SAlasdair G Kergon 	if (!ps->valid)
6644db6bfe0SAlasdair G Kergon 		return 1;
6654db6bfe0SAlasdair G Kergon 
6664db6bfe0SAlasdair G Kergon 	/*
6674db6bfe0SAlasdair G Kergon 	 * Read the metadata.
6684db6bfe0SAlasdair G Kergon 	 */
669a159c1acSJonathan Brassow 	r = read_exceptions(ps, callback, callback_context);
6704db6bfe0SAlasdair G Kergon 
671f5acc834SJon Brassow 	return r;
6724db6bfe0SAlasdair G Kergon }
6734db6bfe0SAlasdair G Kergon 
persistent_prepare_exception(struct dm_exception_store * store,struct dm_exception * e)674a159c1acSJonathan Brassow static int persistent_prepare_exception(struct dm_exception_store *store,
6751d4989c8SJon Brassow 					struct dm_exception *e)
6764db6bfe0SAlasdair G Kergon {
6774db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
678fc56f6fbSMike Snitzer 	sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
6794db6bfe0SAlasdair G Kergon 
6804db6bfe0SAlasdair G Kergon 	/* Is there enough room ? */
681d0216849SJonathan Brassow 	if (size < ((ps->next_free + 1) * store->chunk_size))
6824db6bfe0SAlasdair G Kergon 		return -ENOSPC;
6834db6bfe0SAlasdair G Kergon 
6844db6bfe0SAlasdair G Kergon 	e->new_chunk = ps->next_free;
6854db6bfe0SAlasdair G Kergon 
6864db6bfe0SAlasdair G Kergon 	/*
6874db6bfe0SAlasdair G Kergon 	 * Move onto the next free pending, making sure to take
6884db6bfe0SAlasdair G Kergon 	 * into account the location of the metadata chunks.
6894db6bfe0SAlasdair G Kergon 	 */
6904db6bfe0SAlasdair G Kergon 	ps->next_free++;
691e9c6a182SMikulas Patocka 	skip_metadata(ps);
6924db6bfe0SAlasdair G Kergon 
6934db6bfe0SAlasdair G Kergon 	atomic_inc(&ps->pending_count);
6944db6bfe0SAlasdair G Kergon 	return 0;
6954db6bfe0SAlasdair G Kergon }
6964db6bfe0SAlasdair G Kergon 
persistent_commit_exception(struct dm_exception_store * store,struct dm_exception * e,int valid,void (* callback)(void *,int success),void * callback_context)697a159c1acSJonathan Brassow static void persistent_commit_exception(struct dm_exception_store *store,
698385277bfSMikulas Patocka 					struct dm_exception *e, int valid,
6994db6bfe0SAlasdair G Kergon 					void (*callback)(void *, int success),
7004db6bfe0SAlasdair G Kergon 					void *callback_context)
7014db6bfe0SAlasdair G Kergon {
7024db6bfe0SAlasdair G Kergon 	unsigned int i;
7034db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
704283a8328SAlasdair G Kergon 	struct core_exception ce;
7054db6bfe0SAlasdair G Kergon 	struct commit_callback *cb;
7064db6bfe0SAlasdair G Kergon 
707385277bfSMikulas Patocka 	if (!valid)
708385277bfSMikulas Patocka 		ps->valid = 0;
709385277bfSMikulas Patocka 
710283a8328SAlasdair G Kergon 	ce.old_chunk = e->old_chunk;
711283a8328SAlasdair G Kergon 	ce.new_chunk = e->new_chunk;
712283a8328SAlasdair G Kergon 	write_exception(ps, ps->current_committed++, &ce);
7134db6bfe0SAlasdair G Kergon 
7144db6bfe0SAlasdair G Kergon 	/*
7154db6bfe0SAlasdair G Kergon 	 * Add the callback to the back of the array.  This code
7164db6bfe0SAlasdair G Kergon 	 * is the only place where the callback array is
7174db6bfe0SAlasdair G Kergon 	 * manipulated, and we know that it will never be called
7184db6bfe0SAlasdair G Kergon 	 * multiple times concurrently.
7194db6bfe0SAlasdair G Kergon 	 */
7204db6bfe0SAlasdair G Kergon 	cb = ps->callbacks + ps->callback_count++;
7214db6bfe0SAlasdair G Kergon 	cb->callback = callback;
7224db6bfe0SAlasdair G Kergon 	cb->context = callback_context;
7234db6bfe0SAlasdair G Kergon 
7244db6bfe0SAlasdair G Kergon 	/*
7254db6bfe0SAlasdair G Kergon 	 * If there are exceptions in flight and we have not yet
7264db6bfe0SAlasdair G Kergon 	 * filled this metadata area there's nothing more to do.
7274db6bfe0SAlasdair G Kergon 	 */
7284db6bfe0SAlasdair G Kergon 	if (!atomic_dec_and_test(&ps->pending_count) &&
7294db6bfe0SAlasdair G Kergon 	    (ps->current_committed != ps->exceptions_per_area))
7304db6bfe0SAlasdair G Kergon 		return;
7314db6bfe0SAlasdair G Kergon 
7324db6bfe0SAlasdair G Kergon 	/*
7334db6bfe0SAlasdair G Kergon 	 * If we completely filled the current area, then wipe the next one.
7344db6bfe0SAlasdair G Kergon 	 */
7354db6bfe0SAlasdair G Kergon 	if ((ps->current_committed == ps->exceptions_per_area) &&
7364db6bfe0SAlasdair G Kergon 	    zero_disk_area(ps, ps->current_area + 1))
7374db6bfe0SAlasdair G Kergon 		ps->valid = 0;
7384db6bfe0SAlasdair G Kergon 
7394db6bfe0SAlasdair G Kergon 	/*
7404db6bfe0SAlasdair G Kergon 	 * Commit exceptions to disk.
7414db6bfe0SAlasdair G Kergon 	 */
7426b990139SBart Van Assche 	if (ps->valid && area_io(ps, REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA |
7436b990139SBart Van Assche 				 REQ_SYNC))
7444db6bfe0SAlasdair G Kergon 		ps->valid = 0;
7454db6bfe0SAlasdair G Kergon 
7464db6bfe0SAlasdair G Kergon 	/*
7474db6bfe0SAlasdair G Kergon 	 * Advance to the next area if this one is full.
7484db6bfe0SAlasdair G Kergon 	 */
7494db6bfe0SAlasdair G Kergon 	if (ps->current_committed == ps->exceptions_per_area) {
7504db6bfe0SAlasdair G Kergon 		ps->current_committed = 0;
7514db6bfe0SAlasdair G Kergon 		ps->current_area++;
7524db6bfe0SAlasdair G Kergon 		zero_memory_area(ps);
7534db6bfe0SAlasdair G Kergon 	}
7544db6bfe0SAlasdair G Kergon 
7554db6bfe0SAlasdair G Kergon 	for (i = 0; i < ps->callback_count; i++) {
7564db6bfe0SAlasdair G Kergon 		cb = ps->callbacks + i;
7574db6bfe0SAlasdair G Kergon 		cb->callback(cb->context, ps->valid);
7584db6bfe0SAlasdair G Kergon 	}
7594db6bfe0SAlasdair G Kergon 
7604db6bfe0SAlasdair G Kergon 	ps->callback_count = 0;
7614db6bfe0SAlasdair G Kergon }
7624db6bfe0SAlasdair G Kergon 
persistent_prepare_merge(struct dm_exception_store * store,chunk_t * last_old_chunk,chunk_t * last_new_chunk)7634454a621SMikulas Patocka static int persistent_prepare_merge(struct dm_exception_store *store,
7644454a621SMikulas Patocka 				    chunk_t *last_old_chunk,
7654454a621SMikulas Patocka 				    chunk_t *last_new_chunk)
7664454a621SMikulas Patocka {
7674454a621SMikulas Patocka 	struct pstore *ps = get_info(store);
768283a8328SAlasdair G Kergon 	struct core_exception ce;
7694454a621SMikulas Patocka 	int nr_consecutive;
7704454a621SMikulas Patocka 	int r;
7714454a621SMikulas Patocka 
7724454a621SMikulas Patocka 	/*
7734454a621SMikulas Patocka 	 * When current area is empty, move back to preceding area.
7744454a621SMikulas Patocka 	 */
7754454a621SMikulas Patocka 	if (!ps->current_committed) {
7764454a621SMikulas Patocka 		/*
7774454a621SMikulas Patocka 		 * Have we finished?
7784454a621SMikulas Patocka 		 */
7794454a621SMikulas Patocka 		if (!ps->current_area)
7804454a621SMikulas Patocka 			return 0;
7814454a621SMikulas Patocka 
7824454a621SMikulas Patocka 		ps->current_area--;
7836b990139SBart Van Assche 		r = area_io(ps, REQ_OP_READ);
7844454a621SMikulas Patocka 		if (r < 0)
7854454a621SMikulas Patocka 			return r;
7864454a621SMikulas Patocka 		ps->current_committed = ps->exceptions_per_area;
7874454a621SMikulas Patocka 	}
7884454a621SMikulas Patocka 
7892cadabd5SMikulas Patocka 	read_exception(ps, ps->area, ps->current_committed - 1, &ce);
790283a8328SAlasdair G Kergon 	*last_old_chunk = ce.old_chunk;
791283a8328SAlasdair G Kergon 	*last_new_chunk = ce.new_chunk;
7924454a621SMikulas Patocka 
7934454a621SMikulas Patocka 	/*
7944454a621SMikulas Patocka 	 * Find number of consecutive chunks within the current area,
7954454a621SMikulas Patocka 	 * working backwards.
7964454a621SMikulas Patocka 	 */
7974454a621SMikulas Patocka 	for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
7984454a621SMikulas Patocka 	     nr_consecutive++) {
7992cadabd5SMikulas Patocka 		read_exception(ps, ps->area,
8002cadabd5SMikulas Patocka 			       ps->current_committed - 1 - nr_consecutive, &ce);
801283a8328SAlasdair G Kergon 		if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
802283a8328SAlasdair G Kergon 		    ce.new_chunk != *last_new_chunk - nr_consecutive)
8034454a621SMikulas Patocka 			break;
8044454a621SMikulas Patocka 	}
8054454a621SMikulas Patocka 
8064454a621SMikulas Patocka 	return nr_consecutive;
8074454a621SMikulas Patocka }
8084454a621SMikulas Patocka 
persistent_commit_merge(struct dm_exception_store * store,int nr_merged)8094454a621SMikulas Patocka static int persistent_commit_merge(struct dm_exception_store *store,
8104454a621SMikulas Patocka 				   int nr_merged)
8114454a621SMikulas Patocka {
8124454a621SMikulas Patocka 	int r, i;
8134454a621SMikulas Patocka 	struct pstore *ps = get_info(store);
8144454a621SMikulas Patocka 
8154454a621SMikulas Patocka 	BUG_ON(nr_merged > ps->current_committed);
8164454a621SMikulas Patocka 
8174454a621SMikulas Patocka 	for (i = 0; i < nr_merged; i++)
8184454a621SMikulas Patocka 		clear_exception(ps, ps->current_committed - 1 - i);
8194454a621SMikulas Patocka 
8206b990139SBart Van Assche 	r = area_io(ps, REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA);
8214454a621SMikulas Patocka 	if (r < 0)
8224454a621SMikulas Patocka 		return r;
8234454a621SMikulas Patocka 
8244454a621SMikulas Patocka 	ps->current_committed -= nr_merged;
8254454a621SMikulas Patocka 
8264454a621SMikulas Patocka 	/*
8274454a621SMikulas Patocka 	 * At this stage, only persistent_usage() uses ps->next_free, so
8284454a621SMikulas Patocka 	 * we make no attempt to keep ps->next_free strictly accurate
8294454a621SMikulas Patocka 	 * as exceptions may have been committed out-of-order originally.
8304454a621SMikulas Patocka 	 * Once a snapshot has become merging, we set it to the value it
8314454a621SMikulas Patocka 	 * would have held had all the exceptions been committed in order.
8324454a621SMikulas Patocka 	 *
8334454a621SMikulas Patocka 	 * ps->current_area does not get reduced by prepare_merge() until
8344454a621SMikulas Patocka 	 * after commit_merge() has removed the nr_merged previous exceptions.
8354454a621SMikulas Patocka 	 */
83687c961cbSTomohiro Kusumi 	ps->next_free = area_location(ps, ps->current_area) +
83787c961cbSTomohiro Kusumi 			ps->current_committed + 1;
8384454a621SMikulas Patocka 
8394454a621SMikulas Patocka 	return 0;
8404454a621SMikulas Patocka }
8414454a621SMikulas Patocka 
persistent_drop_snapshot(struct dm_exception_store * store)842a159c1acSJonathan Brassow static void persistent_drop_snapshot(struct dm_exception_store *store)
8434db6bfe0SAlasdair G Kergon {
8444db6bfe0SAlasdair G Kergon 	struct pstore *ps = get_info(store);
8454db6bfe0SAlasdair G Kergon 
8464db6bfe0SAlasdair G Kergon 	ps->valid = 0;
8474db6bfe0SAlasdair G Kergon 	if (write_header(ps))
8484db6bfe0SAlasdair G Kergon 		DMWARN("write header failed");
8494db6bfe0SAlasdair G Kergon }
8504db6bfe0SAlasdair G Kergon 
persistent_ctr(struct dm_exception_store * store,char * options)851b0d3cc01SMike Snitzer static int persistent_ctr(struct dm_exception_store *store, char *options)
8524db6bfe0SAlasdair G Kergon {
8534db6bfe0SAlasdair G Kergon 	struct pstore *ps;
854a2a678edSSudip Mukherjee 	int r;
8554db6bfe0SAlasdair G Kergon 
8564db6bfe0SAlasdair G Kergon 	/* allocate the pstore */
857a32079ceSJonathan Brassow 	ps = kzalloc(sizeof(*ps), GFP_KERNEL);
8584db6bfe0SAlasdair G Kergon 	if (!ps)
8594db6bfe0SAlasdair G Kergon 		return -ENOMEM;
8604db6bfe0SAlasdair G Kergon 
86171fab00aSJonathan Brassow 	ps->store = store;
8624db6bfe0SAlasdair G Kergon 	ps->valid = 1;
8634db6bfe0SAlasdair G Kergon 	ps->version = SNAPSHOT_DISK_VERSION;
8644db6bfe0SAlasdair G Kergon 	ps->area = NULL;
86561578dcdSMikulas Patocka 	ps->zero_area = NULL;
86661578dcdSMikulas Patocka 	ps->header_area = NULL;
8674454a621SMikulas Patocka 	ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
8684db6bfe0SAlasdair G Kergon 	ps->current_committed = 0;
8694db6bfe0SAlasdair G Kergon 
8704db6bfe0SAlasdair G Kergon 	ps->callback_count = 0;
8714db6bfe0SAlasdair G Kergon 	atomic_set(&ps->pending_count, 0);
8724db6bfe0SAlasdair G Kergon 	ps->callbacks = NULL;
8734db6bfe0SAlasdair G Kergon 
874239c8dd5STejun Heo 	ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0);
8754db6bfe0SAlasdair G Kergon 	if (!ps->metadata_wq) {
8764db6bfe0SAlasdair G Kergon 		DMERR("couldn't start header metadata update thread");
877a2a678edSSudip Mukherjee 		r = -ENOMEM;
878a2a678edSSudip Mukherjee 		goto err_workqueue;
8794db6bfe0SAlasdair G Kergon 	}
8804db6bfe0SAlasdair G Kergon 
881b0d3cc01SMike Snitzer 	if (options) {
882b0d3cc01SMike Snitzer 		char overflow = toupper(options[0]);
8830ef0b471SHeinz Mauelshagen 
884b0d3cc01SMike Snitzer 		if (overflow == 'O')
885b0d3cc01SMike Snitzer 			store->userspace_supports_overflow = true;
886b0d3cc01SMike Snitzer 		else {
887b0d3cc01SMike Snitzer 			DMERR("Unsupported persistent store option: %s", options);
888a2a678edSSudip Mukherjee 			r = -EINVAL;
889a2a678edSSudip Mukherjee 			goto err_options;
890b0d3cc01SMike Snitzer 		}
891b0d3cc01SMike Snitzer 	}
892b0d3cc01SMike Snitzer 
8934db6bfe0SAlasdair G Kergon 	store->context = ps;
8944db6bfe0SAlasdair G Kergon 
8954db6bfe0SAlasdair G Kergon 	return 0;
896a2a678edSSudip Mukherjee 
897a2a678edSSudip Mukherjee err_options:
898a2a678edSSudip Mukherjee 	destroy_workqueue(ps->metadata_wq);
899a2a678edSSudip Mukherjee err_workqueue:
900a2a678edSSudip Mukherjee 	kfree(ps);
901a2a678edSSudip Mukherjee 
902a2a678edSSudip Mukherjee 	return r;
9034db6bfe0SAlasdair G Kergon }
9044db6bfe0SAlasdair G Kergon 
persistent_status(struct dm_exception_store * store,status_type_t status,char * result,unsigned int maxlen)90586a3238cSHeinz Mauelshagen static unsigned int persistent_status(struct dm_exception_store *store,
906493df71cSJonathan Brassow 				  status_type_t status, char *result,
90786a3238cSHeinz Mauelshagen 				  unsigned int maxlen)
908493df71cSJonathan Brassow {
90986a3238cSHeinz Mauelshagen 	unsigned int sz = 0;
9101e302a92SJonathan Brassow 
9111e302a92SJonathan Brassow 	switch (status) {
9121e302a92SJonathan Brassow 	case STATUSTYPE_INFO:
9131e302a92SJonathan Brassow 		break;
9141e302a92SJonathan Brassow 	case STATUSTYPE_TABLE:
915b0d3cc01SMike Snitzer 		DMEMIT(" %s %llu", store->userspace_supports_overflow ? "PO" : "P",
916b0d3cc01SMike Snitzer 		       (unsigned long long)store->chunk_size);
9178ec45662STushar Sugandhi 		break;
9188ec45662STushar Sugandhi 	case STATUSTYPE_IMA:
9198ec45662STushar Sugandhi 		*result = '\0';
9208ec45662STushar Sugandhi 		break;
9211e302a92SJonathan Brassow 	}
922493df71cSJonathan Brassow 
923493df71cSJonathan Brassow 	return sz;
924493df71cSJonathan Brassow }
925493df71cSJonathan Brassow 
926493df71cSJonathan Brassow static struct dm_exception_store_type _persistent_type = {
927493df71cSJonathan Brassow 	.name = "persistent",
928493df71cSJonathan Brassow 	.module = THIS_MODULE,
929493df71cSJonathan Brassow 	.ctr = persistent_ctr,
930493df71cSJonathan Brassow 	.dtr = persistent_dtr,
931493df71cSJonathan Brassow 	.read_metadata = persistent_read_metadata,
932493df71cSJonathan Brassow 	.prepare_exception = persistent_prepare_exception,
933493df71cSJonathan Brassow 	.commit_exception = persistent_commit_exception,
9344454a621SMikulas Patocka 	.prepare_merge = persistent_prepare_merge,
9354454a621SMikulas Patocka 	.commit_merge = persistent_commit_merge,
936493df71cSJonathan Brassow 	.drop_snapshot = persistent_drop_snapshot,
937985903bbSMike Snitzer 	.usage = persistent_usage,
938493df71cSJonathan Brassow 	.status = persistent_status,
939493df71cSJonathan Brassow };
940493df71cSJonathan Brassow 
941493df71cSJonathan Brassow static struct dm_exception_store_type _persistent_compat_type = {
942493df71cSJonathan Brassow 	.name = "P",
943493df71cSJonathan Brassow 	.module = THIS_MODULE,
944493df71cSJonathan Brassow 	.ctr = persistent_ctr,
945493df71cSJonathan Brassow 	.dtr = persistent_dtr,
946493df71cSJonathan Brassow 	.read_metadata = persistent_read_metadata,
947493df71cSJonathan Brassow 	.prepare_exception = persistent_prepare_exception,
948493df71cSJonathan Brassow 	.commit_exception = persistent_commit_exception,
9494454a621SMikulas Patocka 	.prepare_merge = persistent_prepare_merge,
9504454a621SMikulas Patocka 	.commit_merge = persistent_commit_merge,
951493df71cSJonathan Brassow 	.drop_snapshot = persistent_drop_snapshot,
952985903bbSMike Snitzer 	.usage = persistent_usage,
953493df71cSJonathan Brassow 	.status = persistent_status,
954493df71cSJonathan Brassow };
955493df71cSJonathan Brassow 
dm_persistent_snapshot_init(void)9564db6bfe0SAlasdair G Kergon int dm_persistent_snapshot_init(void)
9574db6bfe0SAlasdair G Kergon {
958493df71cSJonathan Brassow 	int r;
959493df71cSJonathan Brassow 
960493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_persistent_type);
961493df71cSJonathan Brassow 	if (r) {
962493df71cSJonathan Brassow 		DMERR("Unable to register persistent exception store type");
963493df71cSJonathan Brassow 		return r;
964493df71cSJonathan Brassow 	}
965493df71cSJonathan Brassow 
966493df71cSJonathan Brassow 	r = dm_exception_store_type_register(&_persistent_compat_type);
967493df71cSJonathan Brassow 	if (r) {
9682e84fecfSHeinz Mauelshagen 		DMERR("Unable to register old-style persistent exception store type");
969493df71cSJonathan Brassow 		dm_exception_store_type_unregister(&_persistent_type);
970493df71cSJonathan Brassow 		return r;
971493df71cSJonathan Brassow 	}
972493df71cSJonathan Brassow 
973493df71cSJonathan Brassow 	return r;
9744db6bfe0SAlasdair G Kergon }
9754db6bfe0SAlasdair G Kergon 
dm_persistent_snapshot_exit(void)9764db6bfe0SAlasdair G Kergon void dm_persistent_snapshot_exit(void)
9774db6bfe0SAlasdair G Kergon {
978493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_persistent_type);
979493df71cSJonathan Brassow 	dm_exception_store_type_unregister(&_persistent_compat_type);
9804db6bfe0SAlasdair G Kergon }
981