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> 134db6bfe0SAlasdair G Kergon #include <linux/slab.h> 144db6bfe0SAlasdair G Kergon #include <linux/dm-io.h> 154db6bfe0SAlasdair G Kergon 164db6bfe0SAlasdair G Kergon #define DM_MSG_PREFIX "persistent snapshot" 174db6bfe0SAlasdair G Kergon #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */ 184db6bfe0SAlasdair G Kergon 194db6bfe0SAlasdair G Kergon /*----------------------------------------------------------------- 204db6bfe0SAlasdair G Kergon * Persistent snapshots, by persistent we mean that the snapshot 214db6bfe0SAlasdair G Kergon * will survive a reboot. 224db6bfe0SAlasdair G Kergon *---------------------------------------------------------------*/ 234db6bfe0SAlasdair G Kergon 244db6bfe0SAlasdair G Kergon /* 254db6bfe0SAlasdair G Kergon * We need to store a record of which parts of the origin have 264db6bfe0SAlasdair G Kergon * been copied to the snapshot device. The snapshot code 274db6bfe0SAlasdair G Kergon * requires that we copy exception chunks to chunk aligned areas 284db6bfe0SAlasdair G Kergon * of the COW store. It makes sense therefore, to store the 294db6bfe0SAlasdair G Kergon * metadata in chunk size blocks. 304db6bfe0SAlasdair G Kergon * 314db6bfe0SAlasdair G Kergon * There is no backward or forward compatibility implemented, 324db6bfe0SAlasdair G Kergon * snapshots with different disk versions than the kernel will 334db6bfe0SAlasdair G Kergon * not be usable. It is expected that "lvcreate" will blank out 344db6bfe0SAlasdair G Kergon * the start of a fresh COW device before calling the snapshot 354db6bfe0SAlasdair G Kergon * constructor. 364db6bfe0SAlasdair G Kergon * 374db6bfe0SAlasdair G Kergon * The first chunk of the COW device just contains the header. 384db6bfe0SAlasdair G Kergon * After this there is a chunk filled with exception metadata, 394db6bfe0SAlasdair G Kergon * followed by as many exception chunks as can fit in the 404db6bfe0SAlasdair G Kergon * metadata areas. 414db6bfe0SAlasdair G Kergon * 424db6bfe0SAlasdair G Kergon * All on disk structures are in little-endian format. The end 434db6bfe0SAlasdair G Kergon * of the exceptions info is indicated by an exception with a 444db6bfe0SAlasdair G Kergon * new_chunk of 0, which is invalid since it would point to the 454db6bfe0SAlasdair G Kergon * header chunk. 464db6bfe0SAlasdair G Kergon */ 474db6bfe0SAlasdair G Kergon 484db6bfe0SAlasdair G Kergon /* 494db6bfe0SAlasdair G Kergon * Magic for persistent snapshots: "SnAp" - Feeble isn't it. 504db6bfe0SAlasdair G Kergon */ 514db6bfe0SAlasdair G Kergon #define SNAP_MAGIC 0x70416e53 524db6bfe0SAlasdair G Kergon 534db6bfe0SAlasdair G Kergon /* 544db6bfe0SAlasdair G Kergon * The on-disk version of the metadata. 554db6bfe0SAlasdair G Kergon */ 564db6bfe0SAlasdair G Kergon #define SNAPSHOT_DISK_VERSION 1 574db6bfe0SAlasdair G Kergon 584454a621SMikulas Patocka #define NUM_SNAPSHOT_HDR_CHUNKS 1 594454a621SMikulas Patocka 604db6bfe0SAlasdair G Kergon struct disk_header { 614db6bfe0SAlasdair G Kergon uint32_t magic; 624db6bfe0SAlasdair G Kergon 634db6bfe0SAlasdair G Kergon /* 644db6bfe0SAlasdair G Kergon * Is this snapshot valid. There is no way of recovering 654db6bfe0SAlasdair G Kergon * an invalid snapshot. 664db6bfe0SAlasdair G Kergon */ 674db6bfe0SAlasdair G Kergon uint32_t valid; 684db6bfe0SAlasdair G Kergon 694db6bfe0SAlasdair G Kergon /* 704db6bfe0SAlasdair G Kergon * Simple, incrementing version. no backward 714db6bfe0SAlasdair G Kergon * compatibility. 724db6bfe0SAlasdair G Kergon */ 734db6bfe0SAlasdair G Kergon uint32_t version; 744db6bfe0SAlasdair G Kergon 754db6bfe0SAlasdair G Kergon /* In sectors */ 764db6bfe0SAlasdair G Kergon uint32_t chunk_size; 774db6bfe0SAlasdair G Kergon }; 784db6bfe0SAlasdair G Kergon 794db6bfe0SAlasdair G Kergon struct disk_exception { 804db6bfe0SAlasdair G Kergon uint64_t old_chunk; 814db6bfe0SAlasdair G Kergon uint64_t new_chunk; 824db6bfe0SAlasdair G Kergon }; 834db6bfe0SAlasdair G Kergon 844db6bfe0SAlasdair G Kergon struct commit_callback { 854db6bfe0SAlasdair G Kergon void (*callback)(void *, int success); 864db6bfe0SAlasdair G Kergon void *context; 874db6bfe0SAlasdair G Kergon }; 884db6bfe0SAlasdair G Kergon 894db6bfe0SAlasdair G Kergon /* 904db6bfe0SAlasdair G Kergon * The top level structure for a persistent exception store. 914db6bfe0SAlasdair G Kergon */ 924db6bfe0SAlasdair G Kergon struct pstore { 9371fab00aSJonathan Brassow struct dm_exception_store *store; 944db6bfe0SAlasdair G Kergon int version; 954db6bfe0SAlasdair G Kergon int valid; 964db6bfe0SAlasdair G Kergon uint32_t exceptions_per_area; 974db6bfe0SAlasdair G Kergon 984db6bfe0SAlasdair G Kergon /* 994db6bfe0SAlasdair G Kergon * Now that we have an asynchronous kcopyd there is no 1004db6bfe0SAlasdair G Kergon * need for large chunk sizes, so it wont hurt to have a 1014db6bfe0SAlasdair G Kergon * whole chunks worth of metadata in memory at once. 1024db6bfe0SAlasdair G Kergon */ 1034db6bfe0SAlasdair G Kergon void *area; 1044db6bfe0SAlasdair G Kergon 1054db6bfe0SAlasdair G Kergon /* 1064db6bfe0SAlasdair G Kergon * An area of zeros used to clear the next area. 1074db6bfe0SAlasdair G Kergon */ 1084db6bfe0SAlasdair G Kergon void *zero_area; 1094db6bfe0SAlasdair G Kergon 1104db6bfe0SAlasdair G Kergon /* 11161578dcdSMikulas Patocka * An area used for header. The header can be written 11261578dcdSMikulas Patocka * concurrently with metadata (when invalidating the snapshot), 11361578dcdSMikulas Patocka * so it needs a separate buffer. 11461578dcdSMikulas Patocka */ 11561578dcdSMikulas Patocka void *header_area; 11661578dcdSMikulas Patocka 11761578dcdSMikulas Patocka /* 1184db6bfe0SAlasdair G Kergon * Used to keep track of which metadata area the data in 1194db6bfe0SAlasdair G Kergon * 'chunk' refers to. 1204db6bfe0SAlasdair G Kergon */ 1214db6bfe0SAlasdair G Kergon chunk_t current_area; 1224db6bfe0SAlasdair G Kergon 1234db6bfe0SAlasdair G Kergon /* 1244db6bfe0SAlasdair G Kergon * The next free chunk for an exception. 1254454a621SMikulas Patocka * 1264454a621SMikulas Patocka * When creating exceptions, all the chunks here and above are 1274454a621SMikulas Patocka * free. It holds the next chunk to be allocated. On rare 1284454a621SMikulas Patocka * occasions (e.g. after a system crash) holes can be left in 1294454a621SMikulas Patocka * the exception store because chunks can be committed out of 1304454a621SMikulas Patocka * order. 1314454a621SMikulas Patocka * 1324454a621SMikulas Patocka * When merging exceptions, it does not necessarily mean all the 1334454a621SMikulas Patocka * chunks here and above are free. It holds the value it would 1344454a621SMikulas Patocka * have held if all chunks had been committed in order of 1354454a621SMikulas Patocka * allocation. Consequently the value may occasionally be 1364454a621SMikulas Patocka * slightly too low, but since it's only used for 'status' and 1374454a621SMikulas Patocka * it can never reach its minimum value too early this doesn't 1384454a621SMikulas Patocka * matter. 1394db6bfe0SAlasdair G Kergon */ 1404454a621SMikulas Patocka 1414db6bfe0SAlasdair G Kergon chunk_t next_free; 1424db6bfe0SAlasdair G Kergon 1434db6bfe0SAlasdair G Kergon /* 1444db6bfe0SAlasdair G Kergon * The index of next free exception in the current 1454db6bfe0SAlasdair G Kergon * metadata area. 1464db6bfe0SAlasdair G Kergon */ 1474db6bfe0SAlasdair G Kergon uint32_t current_committed; 1484db6bfe0SAlasdair G Kergon 1494db6bfe0SAlasdair G Kergon atomic_t pending_count; 1504db6bfe0SAlasdair G Kergon uint32_t callback_count; 1514db6bfe0SAlasdair G Kergon struct commit_callback *callbacks; 1524db6bfe0SAlasdair G Kergon struct dm_io_client *io_client; 1534db6bfe0SAlasdair G Kergon 1544db6bfe0SAlasdair G Kergon struct workqueue_struct *metadata_wq; 1554db6bfe0SAlasdair G Kergon }; 1564db6bfe0SAlasdair G Kergon 1574db6bfe0SAlasdair G Kergon static int alloc_area(struct pstore *ps) 1584db6bfe0SAlasdair G Kergon { 1594db6bfe0SAlasdair G Kergon int r = -ENOMEM; 1604db6bfe0SAlasdair G Kergon size_t len; 1614db6bfe0SAlasdair G Kergon 16271fab00aSJonathan Brassow len = ps->store->chunk_size << SECTOR_SHIFT; 1634db6bfe0SAlasdair G Kergon 1644db6bfe0SAlasdair G Kergon /* 1654db6bfe0SAlasdair G Kergon * Allocate the chunk_size block of memory that will hold 1664db6bfe0SAlasdair G Kergon * a single metadata area. 1674db6bfe0SAlasdair G Kergon */ 1684db6bfe0SAlasdair G Kergon ps->area = vmalloc(len); 1694db6bfe0SAlasdair G Kergon if (!ps->area) 17061578dcdSMikulas Patocka goto err_area; 1714db6bfe0SAlasdair G Kergon 1724db6bfe0SAlasdair G Kergon ps->zero_area = vmalloc(len); 17361578dcdSMikulas Patocka if (!ps->zero_area) 17461578dcdSMikulas Patocka goto err_zero_area; 1754db6bfe0SAlasdair G Kergon memset(ps->zero_area, 0, len); 1764db6bfe0SAlasdair G Kergon 17761578dcdSMikulas Patocka ps->header_area = vmalloc(len); 17861578dcdSMikulas Patocka if (!ps->header_area) 17961578dcdSMikulas Patocka goto err_header_area; 18061578dcdSMikulas Patocka 1814db6bfe0SAlasdair G Kergon return 0; 18261578dcdSMikulas Patocka 18361578dcdSMikulas Patocka err_header_area: 18461578dcdSMikulas Patocka vfree(ps->zero_area); 18561578dcdSMikulas Patocka 18661578dcdSMikulas Patocka err_zero_area: 18761578dcdSMikulas Patocka vfree(ps->area); 18861578dcdSMikulas Patocka 18961578dcdSMikulas Patocka err_area: 19061578dcdSMikulas Patocka return r; 1914db6bfe0SAlasdair G Kergon } 1924db6bfe0SAlasdair G Kergon 1934db6bfe0SAlasdair G Kergon static void free_area(struct pstore *ps) 1944db6bfe0SAlasdair G Kergon { 195a32079ceSJonathan Brassow if (ps->area) 1964db6bfe0SAlasdair G Kergon vfree(ps->area); 1974db6bfe0SAlasdair G Kergon ps->area = NULL; 198a32079ceSJonathan Brassow 199a32079ceSJonathan Brassow if (ps->zero_area) 2004db6bfe0SAlasdair G Kergon vfree(ps->zero_area); 2014db6bfe0SAlasdair G Kergon ps->zero_area = NULL; 20261578dcdSMikulas Patocka 20361578dcdSMikulas Patocka if (ps->header_area) 20461578dcdSMikulas Patocka vfree(ps->header_area); 20561578dcdSMikulas Patocka ps->header_area = NULL; 2064db6bfe0SAlasdair G Kergon } 2074db6bfe0SAlasdair G Kergon 2084db6bfe0SAlasdair G Kergon struct mdata_req { 2094db6bfe0SAlasdair G Kergon struct dm_io_region *where; 2104db6bfe0SAlasdair G Kergon struct dm_io_request *io_req; 2114db6bfe0SAlasdair G Kergon struct work_struct work; 2124db6bfe0SAlasdair G Kergon int result; 2134db6bfe0SAlasdair G Kergon }; 2144db6bfe0SAlasdair G Kergon 2154db6bfe0SAlasdair G Kergon static void do_metadata(struct work_struct *work) 2164db6bfe0SAlasdair G Kergon { 2174db6bfe0SAlasdair G Kergon struct mdata_req *req = container_of(work, struct mdata_req, work); 2184db6bfe0SAlasdair G Kergon 2194db6bfe0SAlasdair G Kergon req->result = dm_io(req->io_req, 1, req->where, NULL); 2204db6bfe0SAlasdair G Kergon } 2214db6bfe0SAlasdair G Kergon 2224db6bfe0SAlasdair G Kergon /* 2234db6bfe0SAlasdair G Kergon * Read or write a chunk aligned and sized block of data from a device. 2244db6bfe0SAlasdair G Kergon */ 22502d2fd31SMikulas Patocka static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, int rw, 22602d2fd31SMikulas Patocka int metadata) 2274db6bfe0SAlasdair G Kergon { 2284db6bfe0SAlasdair G Kergon struct dm_io_region where = { 229fc56f6fbSMike Snitzer .bdev = dm_snap_cow(ps->store->snap)->bdev, 23071fab00aSJonathan Brassow .sector = ps->store->chunk_size * chunk, 23171fab00aSJonathan Brassow .count = ps->store->chunk_size, 2324db6bfe0SAlasdair G Kergon }; 2334db6bfe0SAlasdair G Kergon struct dm_io_request io_req = { 2344db6bfe0SAlasdair G Kergon .bi_rw = rw, 2354db6bfe0SAlasdair G Kergon .mem.type = DM_IO_VMA, 23602d2fd31SMikulas Patocka .mem.ptr.vma = area, 2374db6bfe0SAlasdair G Kergon .client = ps->io_client, 2384db6bfe0SAlasdair G Kergon .notify.fn = NULL, 2394db6bfe0SAlasdair G Kergon }; 2404db6bfe0SAlasdair G Kergon struct mdata_req req; 2414db6bfe0SAlasdair G Kergon 2424db6bfe0SAlasdair G Kergon if (!metadata) 2434db6bfe0SAlasdair G Kergon return dm_io(&io_req, 1, &where, NULL); 2444db6bfe0SAlasdair G Kergon 2454db6bfe0SAlasdair G Kergon req.where = &where; 2464db6bfe0SAlasdair G Kergon req.io_req = &io_req; 2474db6bfe0SAlasdair G Kergon 2484db6bfe0SAlasdair G Kergon /* 2494db6bfe0SAlasdair G Kergon * Issue the synchronous I/O from a different thread 2504db6bfe0SAlasdair G Kergon * to avoid generic_make_request recursion. 2514db6bfe0SAlasdair G Kergon */ 252ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&req.work, do_metadata); 2534db6bfe0SAlasdair G Kergon queue_work(ps->metadata_wq, &req.work); 254239c8dd5STejun Heo flush_work(&req.work); 2554db6bfe0SAlasdair G Kergon 2564db6bfe0SAlasdair G Kergon return req.result; 2574db6bfe0SAlasdair G Kergon } 2584db6bfe0SAlasdair G Kergon 2594db6bfe0SAlasdair G Kergon /* 2604db6bfe0SAlasdair G Kergon * Convert a metadata area index to a chunk index. 2614db6bfe0SAlasdair G Kergon */ 2624db6bfe0SAlasdair G Kergon static chunk_t area_location(struct pstore *ps, chunk_t area) 2634db6bfe0SAlasdair G Kergon { 26487c961cbSTomohiro Kusumi return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area); 2654db6bfe0SAlasdair G Kergon } 2664db6bfe0SAlasdair G Kergon 2674db6bfe0SAlasdair G Kergon /* 2684db6bfe0SAlasdair G Kergon * Read or write a metadata area. Remembering to skip the first 2694db6bfe0SAlasdair G Kergon * chunk which holds the header. 2704db6bfe0SAlasdair G Kergon */ 2714db6bfe0SAlasdair G Kergon static int area_io(struct pstore *ps, int rw) 2724db6bfe0SAlasdair G Kergon { 2734db6bfe0SAlasdair G Kergon int r; 2744db6bfe0SAlasdair G Kergon chunk_t chunk; 2754db6bfe0SAlasdair G Kergon 2764db6bfe0SAlasdair G Kergon chunk = area_location(ps, ps->current_area); 2774db6bfe0SAlasdair G Kergon 27802d2fd31SMikulas Patocka r = chunk_io(ps, ps->area, chunk, rw, 0); 2794db6bfe0SAlasdair G Kergon if (r) 2804db6bfe0SAlasdair G Kergon return r; 2814db6bfe0SAlasdair G Kergon 2824db6bfe0SAlasdair G Kergon return 0; 2834db6bfe0SAlasdair G Kergon } 2844db6bfe0SAlasdair G Kergon 2854db6bfe0SAlasdair G Kergon static void zero_memory_area(struct pstore *ps) 2864db6bfe0SAlasdair G Kergon { 28771fab00aSJonathan Brassow memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT); 2884db6bfe0SAlasdair G Kergon } 2894db6bfe0SAlasdair G Kergon 2904db6bfe0SAlasdair G Kergon static int zero_disk_area(struct pstore *ps, chunk_t area) 2914db6bfe0SAlasdair G Kergon { 29202d2fd31SMikulas Patocka return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0); 2934db6bfe0SAlasdair G Kergon } 2944db6bfe0SAlasdair G Kergon 2954db6bfe0SAlasdair G Kergon static int read_header(struct pstore *ps, int *new_snapshot) 2964db6bfe0SAlasdair G Kergon { 2974db6bfe0SAlasdair G Kergon int r; 2984db6bfe0SAlasdair G Kergon struct disk_header *dh; 299df96eee6SMikulas Patocka unsigned chunk_size; 3004db6bfe0SAlasdair G Kergon int chunk_size_supplied = 1; 301ae0b7448SMikulas Patocka char *chunk_err; 3024db6bfe0SAlasdair G Kergon 3034db6bfe0SAlasdair G Kergon /* 304df96eee6SMikulas Patocka * Use default chunk size (or logical_block_size, if larger) 305df96eee6SMikulas Patocka * if none supplied 3064db6bfe0SAlasdair G Kergon */ 30771fab00aSJonathan Brassow if (!ps->store->chunk_size) { 30871fab00aSJonathan Brassow ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS, 309fc56f6fbSMike Snitzer bdev_logical_block_size(dm_snap_cow(ps->store->snap)-> 310fc56f6fbSMike Snitzer bdev) >> 9); 31171fab00aSJonathan Brassow ps->store->chunk_mask = ps->store->chunk_size - 1; 31271fab00aSJonathan Brassow ps->store->chunk_shift = ffs(ps->store->chunk_size) - 1; 3134db6bfe0SAlasdair G Kergon chunk_size_supplied = 0; 3144db6bfe0SAlasdair G Kergon } 3154db6bfe0SAlasdair G Kergon 316*bda8efecSMikulas Patocka ps->io_client = dm_io_client_create(); 3174db6bfe0SAlasdair G Kergon if (IS_ERR(ps->io_client)) 3184db6bfe0SAlasdair G Kergon return PTR_ERR(ps->io_client); 3194db6bfe0SAlasdair G Kergon 3204db6bfe0SAlasdair G Kergon r = alloc_area(ps); 3214db6bfe0SAlasdair G Kergon if (r) 3224db6bfe0SAlasdair G Kergon return r; 3234db6bfe0SAlasdair G Kergon 32461578dcdSMikulas Patocka r = chunk_io(ps, ps->header_area, 0, READ, 1); 3254db6bfe0SAlasdair G Kergon if (r) 3264db6bfe0SAlasdair G Kergon goto bad; 3274db6bfe0SAlasdair G Kergon 32861578dcdSMikulas Patocka dh = ps->header_area; 3294db6bfe0SAlasdair G Kergon 3304db6bfe0SAlasdair G Kergon if (le32_to_cpu(dh->magic) == 0) { 3314db6bfe0SAlasdair G Kergon *new_snapshot = 1; 3324db6bfe0SAlasdair G Kergon return 0; 3334db6bfe0SAlasdair G Kergon } 3344db6bfe0SAlasdair G Kergon 3354db6bfe0SAlasdair G Kergon if (le32_to_cpu(dh->magic) != SNAP_MAGIC) { 3364db6bfe0SAlasdair G Kergon DMWARN("Invalid or corrupt snapshot"); 3374db6bfe0SAlasdair G Kergon r = -ENXIO; 3384db6bfe0SAlasdair G Kergon goto bad; 3394db6bfe0SAlasdair G Kergon } 3404db6bfe0SAlasdair G Kergon 3414db6bfe0SAlasdair G Kergon *new_snapshot = 0; 3424db6bfe0SAlasdair G Kergon ps->valid = le32_to_cpu(dh->valid); 3434db6bfe0SAlasdair G Kergon ps->version = le32_to_cpu(dh->version); 3444db6bfe0SAlasdair G Kergon chunk_size = le32_to_cpu(dh->chunk_size); 3454db6bfe0SAlasdair G Kergon 346ae0b7448SMikulas Patocka if (ps->store->chunk_size == chunk_size) 3474db6bfe0SAlasdair G Kergon return 0; 3484db6bfe0SAlasdair G Kergon 349ae0b7448SMikulas Patocka if (chunk_size_supplied) 350df96eee6SMikulas Patocka DMWARN("chunk size %u in device metadata overrides " 351df96eee6SMikulas Patocka "table chunk size of %u.", 352df96eee6SMikulas Patocka chunk_size, ps->store->chunk_size); 3534db6bfe0SAlasdair G Kergon 3544db6bfe0SAlasdair G Kergon /* We had a bogus chunk_size. Fix stuff up. */ 3554db6bfe0SAlasdair G Kergon free_area(ps); 3564db6bfe0SAlasdair G Kergon 357ae0b7448SMikulas Patocka r = dm_exception_store_set_chunk_size(ps->store, chunk_size, 358ae0b7448SMikulas Patocka &chunk_err); 359ae0b7448SMikulas Patocka if (r) { 360df96eee6SMikulas Patocka DMERR("invalid on-disk chunk size %u: %s.", 361df96eee6SMikulas Patocka chunk_size, chunk_err); 362ae0b7448SMikulas Patocka return r; 363ae0b7448SMikulas Patocka } 3644db6bfe0SAlasdair G Kergon 3654db6bfe0SAlasdair G Kergon r = alloc_area(ps); 3664db6bfe0SAlasdair G Kergon return r; 3674db6bfe0SAlasdair G Kergon 3684db6bfe0SAlasdair G Kergon bad: 3694db6bfe0SAlasdair G Kergon free_area(ps); 3704db6bfe0SAlasdair G Kergon return r; 3714db6bfe0SAlasdair G Kergon } 3724db6bfe0SAlasdair G Kergon 3734db6bfe0SAlasdair G Kergon static int write_header(struct pstore *ps) 3744db6bfe0SAlasdair G Kergon { 3754db6bfe0SAlasdair G Kergon struct disk_header *dh; 3764db6bfe0SAlasdair G Kergon 37761578dcdSMikulas Patocka memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT); 3784db6bfe0SAlasdair G Kergon 37961578dcdSMikulas Patocka dh = ps->header_area; 3804db6bfe0SAlasdair G Kergon dh->magic = cpu_to_le32(SNAP_MAGIC); 3814db6bfe0SAlasdair G Kergon dh->valid = cpu_to_le32(ps->valid); 3824db6bfe0SAlasdair G Kergon dh->version = cpu_to_le32(ps->version); 38371fab00aSJonathan Brassow dh->chunk_size = cpu_to_le32(ps->store->chunk_size); 3844db6bfe0SAlasdair G Kergon 38561578dcdSMikulas Patocka return chunk_io(ps, ps->header_area, 0, WRITE, 1); 3864db6bfe0SAlasdair G Kergon } 3874db6bfe0SAlasdair G Kergon 3884db6bfe0SAlasdair G Kergon /* 3894db6bfe0SAlasdair G Kergon * Access functions for the disk exceptions, these do the endian conversions. 3904db6bfe0SAlasdair G Kergon */ 3914db6bfe0SAlasdair G Kergon static struct disk_exception *get_exception(struct pstore *ps, uint32_t index) 3924db6bfe0SAlasdair G Kergon { 3934db6bfe0SAlasdair G Kergon BUG_ON(index >= ps->exceptions_per_area); 3944db6bfe0SAlasdair G Kergon 3954db6bfe0SAlasdair G Kergon return ((struct disk_exception *) ps->area) + index; 3964db6bfe0SAlasdair G Kergon } 3974db6bfe0SAlasdair G Kergon 3984db6bfe0SAlasdair G Kergon static void read_exception(struct pstore *ps, 3994db6bfe0SAlasdair G Kergon uint32_t index, struct disk_exception *result) 4004db6bfe0SAlasdair G Kergon { 4014db6bfe0SAlasdair G Kergon struct disk_exception *e = get_exception(ps, index); 4024db6bfe0SAlasdair G Kergon 4034db6bfe0SAlasdair G Kergon /* copy it */ 4044db6bfe0SAlasdair G Kergon result->old_chunk = le64_to_cpu(e->old_chunk); 4054db6bfe0SAlasdair G Kergon result->new_chunk = le64_to_cpu(e->new_chunk); 4064db6bfe0SAlasdair G Kergon } 4074db6bfe0SAlasdair G Kergon 4084db6bfe0SAlasdair G Kergon static void write_exception(struct pstore *ps, 4094db6bfe0SAlasdair G Kergon uint32_t index, struct disk_exception *de) 4104db6bfe0SAlasdair G Kergon { 4114db6bfe0SAlasdair G Kergon struct disk_exception *e = get_exception(ps, index); 4124db6bfe0SAlasdair G Kergon 4134db6bfe0SAlasdair G Kergon /* copy it */ 4144db6bfe0SAlasdair G Kergon e->old_chunk = cpu_to_le64(de->old_chunk); 4154db6bfe0SAlasdair G Kergon e->new_chunk = cpu_to_le64(de->new_chunk); 4164db6bfe0SAlasdair G Kergon } 4174db6bfe0SAlasdair G Kergon 4184454a621SMikulas Patocka static void clear_exception(struct pstore *ps, uint32_t index) 4194454a621SMikulas Patocka { 4204454a621SMikulas Patocka struct disk_exception *e = get_exception(ps, index); 4214454a621SMikulas Patocka 4224454a621SMikulas Patocka /* clear it */ 4234454a621SMikulas Patocka e->old_chunk = 0; 4244454a621SMikulas Patocka e->new_chunk = 0; 4254454a621SMikulas Patocka } 4264454a621SMikulas Patocka 4274db6bfe0SAlasdair G Kergon /* 4284db6bfe0SAlasdair G Kergon * Registers the exceptions that are present in the current area. 4294db6bfe0SAlasdair G Kergon * 'full' is filled in to indicate if the area has been 4304db6bfe0SAlasdair G Kergon * filled. 4314db6bfe0SAlasdair G Kergon */ 432a159c1acSJonathan Brassow static int insert_exceptions(struct pstore *ps, 433a159c1acSJonathan Brassow int (*callback)(void *callback_context, 434a159c1acSJonathan Brassow chunk_t old, chunk_t new), 435a159c1acSJonathan Brassow void *callback_context, 436a159c1acSJonathan Brassow int *full) 4374db6bfe0SAlasdair G Kergon { 4384db6bfe0SAlasdair G Kergon int r; 4394db6bfe0SAlasdair G Kergon unsigned int i; 4404db6bfe0SAlasdair G Kergon struct disk_exception de; 4414db6bfe0SAlasdair G Kergon 4424db6bfe0SAlasdair G Kergon /* presume the area is full */ 4434db6bfe0SAlasdair G Kergon *full = 1; 4444db6bfe0SAlasdair G Kergon 4454db6bfe0SAlasdair G Kergon for (i = 0; i < ps->exceptions_per_area; i++) { 4464db6bfe0SAlasdair G Kergon read_exception(ps, i, &de); 4474db6bfe0SAlasdair G Kergon 4484db6bfe0SAlasdair G Kergon /* 4494db6bfe0SAlasdair G Kergon * If the new_chunk is pointing at the start of 4504db6bfe0SAlasdair G Kergon * the COW device, where the first metadata area 4514db6bfe0SAlasdair G Kergon * is we know that we've hit the end of the 4524db6bfe0SAlasdair G Kergon * exceptions. Therefore the area is not full. 4534db6bfe0SAlasdair G Kergon */ 4544db6bfe0SAlasdair G Kergon if (de.new_chunk == 0LL) { 4554db6bfe0SAlasdair G Kergon ps->current_committed = i; 4564db6bfe0SAlasdair G Kergon *full = 0; 4574db6bfe0SAlasdair G Kergon break; 4584db6bfe0SAlasdair G Kergon } 4594db6bfe0SAlasdair G Kergon 4604db6bfe0SAlasdair G Kergon /* 4614db6bfe0SAlasdair G Kergon * Keep track of the start of the free chunks. 4624db6bfe0SAlasdair G Kergon */ 4634db6bfe0SAlasdair G Kergon if (ps->next_free <= de.new_chunk) 4644db6bfe0SAlasdair G Kergon ps->next_free = de.new_chunk + 1; 4654db6bfe0SAlasdair G Kergon 4664db6bfe0SAlasdair G Kergon /* 4674db6bfe0SAlasdair G Kergon * Otherwise we add the exception to the snapshot. 4684db6bfe0SAlasdair G Kergon */ 469a159c1acSJonathan Brassow r = callback(callback_context, de.old_chunk, de.new_chunk); 4704db6bfe0SAlasdair G Kergon if (r) 4714db6bfe0SAlasdair G Kergon return r; 4724db6bfe0SAlasdair G Kergon } 4734db6bfe0SAlasdair G Kergon 4744db6bfe0SAlasdair G Kergon return 0; 4754db6bfe0SAlasdair G Kergon } 4764db6bfe0SAlasdair G Kergon 477a159c1acSJonathan Brassow static int read_exceptions(struct pstore *ps, 478a159c1acSJonathan Brassow int (*callback)(void *callback_context, chunk_t old, 479a159c1acSJonathan Brassow chunk_t new), 480a159c1acSJonathan Brassow void *callback_context) 4814db6bfe0SAlasdair G Kergon { 4824db6bfe0SAlasdair G Kergon int r, full = 1; 4834db6bfe0SAlasdair G Kergon 4844db6bfe0SAlasdair G Kergon /* 4854db6bfe0SAlasdair G Kergon * Keeping reading chunks and inserting exceptions until 4864db6bfe0SAlasdair G Kergon * we find a partially full area. 4874db6bfe0SAlasdair G Kergon */ 4884db6bfe0SAlasdair G Kergon for (ps->current_area = 0; full; ps->current_area++) { 4894db6bfe0SAlasdair G Kergon r = area_io(ps, READ); 4904db6bfe0SAlasdair G Kergon if (r) 4914db6bfe0SAlasdair G Kergon return r; 4924db6bfe0SAlasdair G Kergon 493a159c1acSJonathan Brassow r = insert_exceptions(ps, callback, callback_context, &full); 4944db6bfe0SAlasdair G Kergon if (r) 4954db6bfe0SAlasdair G Kergon return r; 4964db6bfe0SAlasdair G Kergon } 4974db6bfe0SAlasdair G Kergon 4984db6bfe0SAlasdair G Kergon ps->current_area--; 4994db6bfe0SAlasdair G Kergon 5004db6bfe0SAlasdair G Kergon return 0; 5014db6bfe0SAlasdair G Kergon } 5024db6bfe0SAlasdair G Kergon 5034db6bfe0SAlasdair G Kergon static struct pstore *get_info(struct dm_exception_store *store) 5044db6bfe0SAlasdair G Kergon { 5054db6bfe0SAlasdair G Kergon return (struct pstore *) store->context; 5064db6bfe0SAlasdair G Kergon } 5074db6bfe0SAlasdair G Kergon 508985903bbSMike Snitzer static void persistent_usage(struct dm_exception_store *store, 509985903bbSMike Snitzer sector_t *total_sectors, 510985903bbSMike Snitzer sector_t *sectors_allocated, 511985903bbSMike Snitzer sector_t *metadata_sectors) 5124db6bfe0SAlasdair G Kergon { 513985903bbSMike Snitzer struct pstore *ps = get_info(store); 514985903bbSMike Snitzer 515985903bbSMike Snitzer *sectors_allocated = ps->next_free * store->chunk_size; 516fc56f6fbSMike Snitzer *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev); 517985903bbSMike Snitzer 518985903bbSMike Snitzer /* 519985903bbSMike Snitzer * First chunk is the fixed header. 520985903bbSMike Snitzer * Then there are (ps->current_area + 1) metadata chunks, each one 521985903bbSMike Snitzer * separated from the next by ps->exceptions_per_area data chunks. 522985903bbSMike Snitzer */ 5234454a621SMikulas Patocka *metadata_sectors = (ps->current_area + 1 + NUM_SNAPSHOT_HDR_CHUNKS) * 5244454a621SMikulas Patocka store->chunk_size; 5254db6bfe0SAlasdair G Kergon } 5264db6bfe0SAlasdair G Kergon 527493df71cSJonathan Brassow static void persistent_dtr(struct dm_exception_store *store) 5284db6bfe0SAlasdair G Kergon { 5294db6bfe0SAlasdair G Kergon struct pstore *ps = get_info(store); 5304db6bfe0SAlasdair G Kergon 5314db6bfe0SAlasdair G Kergon destroy_workqueue(ps->metadata_wq); 532a32079ceSJonathan Brassow 533a32079ceSJonathan Brassow /* Created in read_header */ 534a32079ceSJonathan Brassow if (ps->io_client) 5354db6bfe0SAlasdair G Kergon dm_io_client_destroy(ps->io_client); 5364db6bfe0SAlasdair G Kergon free_area(ps); 537a32079ceSJonathan Brassow 538a32079ceSJonathan Brassow /* Allocated in persistent_read_metadata */ 539a32079ceSJonathan Brassow if (ps->callbacks) 540a32079ceSJonathan Brassow vfree(ps->callbacks); 541a32079ceSJonathan Brassow 5424db6bfe0SAlasdair G Kergon kfree(ps); 5434db6bfe0SAlasdair G Kergon } 5444db6bfe0SAlasdair G Kergon 545a159c1acSJonathan Brassow static int persistent_read_metadata(struct dm_exception_store *store, 546a159c1acSJonathan Brassow int (*callback)(void *callback_context, 547a159c1acSJonathan Brassow chunk_t old, chunk_t new), 548a159c1acSJonathan Brassow void *callback_context) 5494db6bfe0SAlasdair G Kergon { 5504db6bfe0SAlasdair G Kergon int r, uninitialized_var(new_snapshot); 5514db6bfe0SAlasdair G Kergon struct pstore *ps = get_info(store); 5524db6bfe0SAlasdair G Kergon 5534db6bfe0SAlasdair G Kergon /* 5544db6bfe0SAlasdair G Kergon * Read the snapshot header. 5554db6bfe0SAlasdair G Kergon */ 5564db6bfe0SAlasdair G Kergon r = read_header(ps, &new_snapshot); 5574db6bfe0SAlasdair G Kergon if (r) 5584db6bfe0SAlasdair G Kergon return r; 5594db6bfe0SAlasdair G Kergon 5604db6bfe0SAlasdair G Kergon /* 5614db6bfe0SAlasdair G Kergon * Now we know correct chunk_size, complete the initialisation. 5624db6bfe0SAlasdair G Kergon */ 56371fab00aSJonathan Brassow ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) / 56471fab00aSJonathan Brassow sizeof(struct disk_exception); 5654db6bfe0SAlasdair G Kergon ps->callbacks = dm_vcalloc(ps->exceptions_per_area, 5664db6bfe0SAlasdair G Kergon sizeof(*ps->callbacks)); 5674db6bfe0SAlasdair G Kergon if (!ps->callbacks) 5684db6bfe0SAlasdair G Kergon return -ENOMEM; 5694db6bfe0SAlasdair G Kergon 5704db6bfe0SAlasdair G Kergon /* 5714db6bfe0SAlasdair G Kergon * Do we need to setup a new snapshot ? 5724db6bfe0SAlasdair G Kergon */ 5734db6bfe0SAlasdair G Kergon if (new_snapshot) { 5744db6bfe0SAlasdair G Kergon r = write_header(ps); 5754db6bfe0SAlasdair G Kergon if (r) { 5764db6bfe0SAlasdair G Kergon DMWARN("write_header failed"); 5774db6bfe0SAlasdair G Kergon return r; 5784db6bfe0SAlasdair G Kergon } 5794db6bfe0SAlasdair G Kergon 5804db6bfe0SAlasdair G Kergon ps->current_area = 0; 5814db6bfe0SAlasdair G Kergon zero_memory_area(ps); 5824db6bfe0SAlasdair G Kergon r = zero_disk_area(ps, 0); 583f5acc834SJon Brassow if (r) 5844db6bfe0SAlasdair G Kergon DMWARN("zero_disk_area(0) failed"); 5854db6bfe0SAlasdair G Kergon return r; 5864db6bfe0SAlasdair G Kergon } 5874db6bfe0SAlasdair G Kergon /* 5884db6bfe0SAlasdair G Kergon * Sanity checks. 5894db6bfe0SAlasdair G Kergon */ 5904db6bfe0SAlasdair G Kergon if (ps->version != SNAPSHOT_DISK_VERSION) { 5914db6bfe0SAlasdair G Kergon DMWARN("unable to handle snapshot disk version %d", 5924db6bfe0SAlasdair G Kergon ps->version); 5934db6bfe0SAlasdair G Kergon return -EINVAL; 5944db6bfe0SAlasdair G Kergon } 5954db6bfe0SAlasdair G Kergon 5964db6bfe0SAlasdair G Kergon /* 5974db6bfe0SAlasdair G Kergon * Metadata are valid, but snapshot is invalidated 5984db6bfe0SAlasdair G Kergon */ 5994db6bfe0SAlasdair G Kergon if (!ps->valid) 6004db6bfe0SAlasdair G Kergon return 1; 6014db6bfe0SAlasdair G Kergon 6024db6bfe0SAlasdair G Kergon /* 6034db6bfe0SAlasdair G Kergon * Read the metadata. 6044db6bfe0SAlasdair G Kergon */ 605a159c1acSJonathan Brassow r = read_exceptions(ps, callback, callback_context); 6064db6bfe0SAlasdair G Kergon 607f5acc834SJon Brassow return r; 6084db6bfe0SAlasdair G Kergon } 6094db6bfe0SAlasdair G Kergon 610a159c1acSJonathan Brassow static int persistent_prepare_exception(struct dm_exception_store *store, 6111d4989c8SJon Brassow struct dm_exception *e) 6124db6bfe0SAlasdair G Kergon { 6134db6bfe0SAlasdair G Kergon struct pstore *ps = get_info(store); 6144db6bfe0SAlasdair G Kergon uint32_t stride; 6154db6bfe0SAlasdair G Kergon chunk_t next_free; 616fc56f6fbSMike Snitzer sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev); 6174db6bfe0SAlasdair G Kergon 6184db6bfe0SAlasdair G Kergon /* Is there enough room ? */ 619d0216849SJonathan Brassow if (size < ((ps->next_free + 1) * store->chunk_size)) 6204db6bfe0SAlasdair G Kergon return -ENOSPC; 6214db6bfe0SAlasdair G Kergon 6224db6bfe0SAlasdair G Kergon e->new_chunk = ps->next_free; 6234db6bfe0SAlasdair G Kergon 6244db6bfe0SAlasdair G Kergon /* 6254db6bfe0SAlasdair G Kergon * Move onto the next free pending, making sure to take 6264db6bfe0SAlasdair G Kergon * into account the location of the metadata chunks. 6274db6bfe0SAlasdair G Kergon */ 6284db6bfe0SAlasdair G Kergon stride = (ps->exceptions_per_area + 1); 6294db6bfe0SAlasdair G Kergon next_free = ++ps->next_free; 6304db6bfe0SAlasdair G Kergon if (sector_div(next_free, stride) == 1) 6314db6bfe0SAlasdair G Kergon ps->next_free++; 6324db6bfe0SAlasdair G Kergon 6334db6bfe0SAlasdair G Kergon atomic_inc(&ps->pending_count); 6344db6bfe0SAlasdair G Kergon return 0; 6354db6bfe0SAlasdair G Kergon } 6364db6bfe0SAlasdair G Kergon 637a159c1acSJonathan Brassow static void persistent_commit_exception(struct dm_exception_store *store, 6381d4989c8SJon Brassow struct dm_exception *e, 6394db6bfe0SAlasdair G Kergon void (*callback) (void *, int success), 6404db6bfe0SAlasdair G Kergon void *callback_context) 6414db6bfe0SAlasdair G Kergon { 6424db6bfe0SAlasdair G Kergon unsigned int i; 6434db6bfe0SAlasdair G Kergon struct pstore *ps = get_info(store); 6444db6bfe0SAlasdair G Kergon struct disk_exception de; 6454db6bfe0SAlasdair G Kergon struct commit_callback *cb; 6464db6bfe0SAlasdair G Kergon 6474db6bfe0SAlasdair G Kergon de.old_chunk = e->old_chunk; 6484db6bfe0SAlasdair G Kergon de.new_chunk = e->new_chunk; 6494db6bfe0SAlasdair G Kergon write_exception(ps, ps->current_committed++, &de); 6504db6bfe0SAlasdair G Kergon 6514db6bfe0SAlasdair G Kergon /* 6524db6bfe0SAlasdair G Kergon * Add the callback to the back of the array. This code 6534db6bfe0SAlasdair G Kergon * is the only place where the callback array is 6544db6bfe0SAlasdair G Kergon * manipulated, and we know that it will never be called 6554db6bfe0SAlasdair G Kergon * multiple times concurrently. 6564db6bfe0SAlasdair G Kergon */ 6574db6bfe0SAlasdair G Kergon cb = ps->callbacks + ps->callback_count++; 6584db6bfe0SAlasdair G Kergon cb->callback = callback; 6594db6bfe0SAlasdair G Kergon cb->context = callback_context; 6604db6bfe0SAlasdair G Kergon 6614db6bfe0SAlasdair G Kergon /* 6624db6bfe0SAlasdair G Kergon * If there are exceptions in flight and we have not yet 6634db6bfe0SAlasdair G Kergon * filled this metadata area there's nothing more to do. 6644db6bfe0SAlasdair G Kergon */ 6654db6bfe0SAlasdair G Kergon if (!atomic_dec_and_test(&ps->pending_count) && 6664db6bfe0SAlasdair G Kergon (ps->current_committed != ps->exceptions_per_area)) 6674db6bfe0SAlasdair G Kergon return; 6684db6bfe0SAlasdair G Kergon 6694db6bfe0SAlasdair G Kergon /* 6704db6bfe0SAlasdair G Kergon * If we completely filled the current area, then wipe the next one. 6714db6bfe0SAlasdair G Kergon */ 6724db6bfe0SAlasdair G Kergon if ((ps->current_committed == ps->exceptions_per_area) && 6734db6bfe0SAlasdair G Kergon zero_disk_area(ps, ps->current_area + 1)) 6744db6bfe0SAlasdair G Kergon ps->valid = 0; 6754db6bfe0SAlasdair G Kergon 6764db6bfe0SAlasdair G Kergon /* 6774db6bfe0SAlasdair G Kergon * Commit exceptions to disk. 6784db6bfe0SAlasdair G Kergon */ 679d87f4c14STejun Heo if (ps->valid && area_io(ps, WRITE_FLUSH_FUA)) 6804db6bfe0SAlasdair G Kergon ps->valid = 0; 6814db6bfe0SAlasdair G Kergon 6824db6bfe0SAlasdair G Kergon /* 6834db6bfe0SAlasdair G Kergon * Advance to the next area if this one is full. 6844db6bfe0SAlasdair G Kergon */ 6854db6bfe0SAlasdair G Kergon if (ps->current_committed == ps->exceptions_per_area) { 6864db6bfe0SAlasdair G Kergon ps->current_committed = 0; 6874db6bfe0SAlasdair G Kergon ps->current_area++; 6884db6bfe0SAlasdair G Kergon zero_memory_area(ps); 6894db6bfe0SAlasdair G Kergon } 6904db6bfe0SAlasdair G Kergon 6914db6bfe0SAlasdair G Kergon for (i = 0; i < ps->callback_count; i++) { 6924db6bfe0SAlasdair G Kergon cb = ps->callbacks + i; 6934db6bfe0SAlasdair G Kergon cb->callback(cb->context, ps->valid); 6944db6bfe0SAlasdair G Kergon } 6954db6bfe0SAlasdair G Kergon 6964db6bfe0SAlasdair G Kergon ps->callback_count = 0; 6974db6bfe0SAlasdair G Kergon } 6984db6bfe0SAlasdair G Kergon 6994454a621SMikulas Patocka static int persistent_prepare_merge(struct dm_exception_store *store, 7004454a621SMikulas Patocka chunk_t *last_old_chunk, 7014454a621SMikulas Patocka chunk_t *last_new_chunk) 7024454a621SMikulas Patocka { 7034454a621SMikulas Patocka struct pstore *ps = get_info(store); 7044454a621SMikulas Patocka struct disk_exception de; 7054454a621SMikulas Patocka int nr_consecutive; 7064454a621SMikulas Patocka int r; 7074454a621SMikulas Patocka 7084454a621SMikulas Patocka /* 7094454a621SMikulas Patocka * When current area is empty, move back to preceding area. 7104454a621SMikulas Patocka */ 7114454a621SMikulas Patocka if (!ps->current_committed) { 7124454a621SMikulas Patocka /* 7134454a621SMikulas Patocka * Have we finished? 7144454a621SMikulas Patocka */ 7154454a621SMikulas Patocka if (!ps->current_area) 7164454a621SMikulas Patocka return 0; 7174454a621SMikulas Patocka 7184454a621SMikulas Patocka ps->current_area--; 7194454a621SMikulas Patocka r = area_io(ps, READ); 7204454a621SMikulas Patocka if (r < 0) 7214454a621SMikulas Patocka return r; 7224454a621SMikulas Patocka ps->current_committed = ps->exceptions_per_area; 7234454a621SMikulas Patocka } 7244454a621SMikulas Patocka 7254454a621SMikulas Patocka read_exception(ps, ps->current_committed - 1, &de); 7264454a621SMikulas Patocka *last_old_chunk = de.old_chunk; 7274454a621SMikulas Patocka *last_new_chunk = de.new_chunk; 7284454a621SMikulas Patocka 7294454a621SMikulas Patocka /* 7304454a621SMikulas Patocka * Find number of consecutive chunks within the current area, 7314454a621SMikulas Patocka * working backwards. 7324454a621SMikulas Patocka */ 7334454a621SMikulas Patocka for (nr_consecutive = 1; nr_consecutive < ps->current_committed; 7344454a621SMikulas Patocka nr_consecutive++) { 7354454a621SMikulas Patocka read_exception(ps, ps->current_committed - 1 - nr_consecutive, 7364454a621SMikulas Patocka &de); 7374454a621SMikulas Patocka if (de.old_chunk != *last_old_chunk - nr_consecutive || 7384454a621SMikulas Patocka de.new_chunk != *last_new_chunk - nr_consecutive) 7394454a621SMikulas Patocka break; 7404454a621SMikulas Patocka } 7414454a621SMikulas Patocka 7424454a621SMikulas Patocka return nr_consecutive; 7434454a621SMikulas Patocka } 7444454a621SMikulas Patocka 7454454a621SMikulas Patocka static int persistent_commit_merge(struct dm_exception_store *store, 7464454a621SMikulas Patocka int nr_merged) 7474454a621SMikulas Patocka { 7484454a621SMikulas Patocka int r, i; 7494454a621SMikulas Patocka struct pstore *ps = get_info(store); 7504454a621SMikulas Patocka 7514454a621SMikulas Patocka BUG_ON(nr_merged > ps->current_committed); 7524454a621SMikulas Patocka 7534454a621SMikulas Patocka for (i = 0; i < nr_merged; i++) 7544454a621SMikulas Patocka clear_exception(ps, ps->current_committed - 1 - i); 7554454a621SMikulas Patocka 7564454a621SMikulas Patocka r = area_io(ps, WRITE); 7574454a621SMikulas Patocka if (r < 0) 7584454a621SMikulas Patocka return r; 7594454a621SMikulas Patocka 7604454a621SMikulas Patocka ps->current_committed -= nr_merged; 7614454a621SMikulas Patocka 7624454a621SMikulas Patocka /* 7634454a621SMikulas Patocka * At this stage, only persistent_usage() uses ps->next_free, so 7644454a621SMikulas Patocka * we make no attempt to keep ps->next_free strictly accurate 7654454a621SMikulas Patocka * as exceptions may have been committed out-of-order originally. 7664454a621SMikulas Patocka * Once a snapshot has become merging, we set it to the value it 7674454a621SMikulas Patocka * would have held had all the exceptions been committed in order. 7684454a621SMikulas Patocka * 7694454a621SMikulas Patocka * ps->current_area does not get reduced by prepare_merge() until 7704454a621SMikulas Patocka * after commit_merge() has removed the nr_merged previous exceptions. 7714454a621SMikulas Patocka */ 77287c961cbSTomohiro Kusumi ps->next_free = area_location(ps, ps->current_area) + 77387c961cbSTomohiro Kusumi ps->current_committed + 1; 7744454a621SMikulas Patocka 7754454a621SMikulas Patocka return 0; 7764454a621SMikulas Patocka } 7774454a621SMikulas Patocka 778a159c1acSJonathan Brassow static void persistent_drop_snapshot(struct dm_exception_store *store) 7794db6bfe0SAlasdair G Kergon { 7804db6bfe0SAlasdair G Kergon struct pstore *ps = get_info(store); 7814db6bfe0SAlasdair G Kergon 7824db6bfe0SAlasdair G Kergon ps->valid = 0; 7834db6bfe0SAlasdair G Kergon if (write_header(ps)) 7844db6bfe0SAlasdair G Kergon DMWARN("write header failed"); 7854db6bfe0SAlasdair G Kergon } 7864db6bfe0SAlasdair G Kergon 787493df71cSJonathan Brassow static int persistent_ctr(struct dm_exception_store *store, 788493df71cSJonathan Brassow unsigned argc, char **argv) 7894db6bfe0SAlasdair G Kergon { 7904db6bfe0SAlasdair G Kergon struct pstore *ps; 7914db6bfe0SAlasdair G Kergon 7924db6bfe0SAlasdair G Kergon /* allocate the pstore */ 793a32079ceSJonathan Brassow ps = kzalloc(sizeof(*ps), GFP_KERNEL); 7944db6bfe0SAlasdair G Kergon if (!ps) 7954db6bfe0SAlasdair G Kergon return -ENOMEM; 7964db6bfe0SAlasdair G Kergon 79771fab00aSJonathan Brassow ps->store = store; 7984db6bfe0SAlasdair G Kergon ps->valid = 1; 7994db6bfe0SAlasdair G Kergon ps->version = SNAPSHOT_DISK_VERSION; 8004db6bfe0SAlasdair G Kergon ps->area = NULL; 80161578dcdSMikulas Patocka ps->zero_area = NULL; 80261578dcdSMikulas Patocka ps->header_area = NULL; 8034454a621SMikulas Patocka ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */ 8044db6bfe0SAlasdair G Kergon ps->current_committed = 0; 8054db6bfe0SAlasdair G Kergon 8064db6bfe0SAlasdair G Kergon ps->callback_count = 0; 8074db6bfe0SAlasdair G Kergon atomic_set(&ps->pending_count, 0); 8084db6bfe0SAlasdair G Kergon ps->callbacks = NULL; 8094db6bfe0SAlasdair G Kergon 810239c8dd5STejun Heo ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0); 8114db6bfe0SAlasdair G Kergon if (!ps->metadata_wq) { 8124db6bfe0SAlasdair G Kergon kfree(ps); 8134db6bfe0SAlasdair G Kergon DMERR("couldn't start header metadata update thread"); 8144db6bfe0SAlasdair G Kergon return -ENOMEM; 8154db6bfe0SAlasdair G Kergon } 8164db6bfe0SAlasdair G Kergon 8174db6bfe0SAlasdair G Kergon store->context = ps; 8184db6bfe0SAlasdair G Kergon 8194db6bfe0SAlasdair G Kergon return 0; 8204db6bfe0SAlasdair G Kergon } 8214db6bfe0SAlasdair G Kergon 8221e302a92SJonathan Brassow static unsigned persistent_status(struct dm_exception_store *store, 823493df71cSJonathan Brassow status_type_t status, char *result, 8241e302a92SJonathan Brassow unsigned maxlen) 825493df71cSJonathan Brassow { 8261e302a92SJonathan Brassow unsigned sz = 0; 8271e302a92SJonathan Brassow 8281e302a92SJonathan Brassow switch (status) { 8291e302a92SJonathan Brassow case STATUSTYPE_INFO: 8301e302a92SJonathan Brassow break; 8311e302a92SJonathan Brassow case STATUSTYPE_TABLE: 832fc56f6fbSMike Snitzer DMEMIT(" P %llu", (unsigned long long)store->chunk_size); 8331e302a92SJonathan Brassow } 834493df71cSJonathan Brassow 835493df71cSJonathan Brassow return sz; 836493df71cSJonathan Brassow } 837493df71cSJonathan Brassow 838493df71cSJonathan Brassow static struct dm_exception_store_type _persistent_type = { 839493df71cSJonathan Brassow .name = "persistent", 840493df71cSJonathan Brassow .module = THIS_MODULE, 841493df71cSJonathan Brassow .ctr = persistent_ctr, 842493df71cSJonathan Brassow .dtr = persistent_dtr, 843493df71cSJonathan Brassow .read_metadata = persistent_read_metadata, 844493df71cSJonathan Brassow .prepare_exception = persistent_prepare_exception, 845493df71cSJonathan Brassow .commit_exception = persistent_commit_exception, 8464454a621SMikulas Patocka .prepare_merge = persistent_prepare_merge, 8474454a621SMikulas Patocka .commit_merge = persistent_commit_merge, 848493df71cSJonathan Brassow .drop_snapshot = persistent_drop_snapshot, 849985903bbSMike Snitzer .usage = persistent_usage, 850493df71cSJonathan Brassow .status = persistent_status, 851493df71cSJonathan Brassow }; 852493df71cSJonathan Brassow 853493df71cSJonathan Brassow static struct dm_exception_store_type _persistent_compat_type = { 854493df71cSJonathan Brassow .name = "P", 855493df71cSJonathan Brassow .module = THIS_MODULE, 856493df71cSJonathan Brassow .ctr = persistent_ctr, 857493df71cSJonathan Brassow .dtr = persistent_dtr, 858493df71cSJonathan Brassow .read_metadata = persistent_read_metadata, 859493df71cSJonathan Brassow .prepare_exception = persistent_prepare_exception, 860493df71cSJonathan Brassow .commit_exception = persistent_commit_exception, 8614454a621SMikulas Patocka .prepare_merge = persistent_prepare_merge, 8624454a621SMikulas Patocka .commit_merge = persistent_commit_merge, 863493df71cSJonathan Brassow .drop_snapshot = persistent_drop_snapshot, 864985903bbSMike Snitzer .usage = persistent_usage, 865493df71cSJonathan Brassow .status = persistent_status, 866493df71cSJonathan Brassow }; 867493df71cSJonathan Brassow 8684db6bfe0SAlasdair G Kergon int dm_persistent_snapshot_init(void) 8694db6bfe0SAlasdair G Kergon { 870493df71cSJonathan Brassow int r; 871493df71cSJonathan Brassow 872493df71cSJonathan Brassow r = dm_exception_store_type_register(&_persistent_type); 873493df71cSJonathan Brassow if (r) { 874493df71cSJonathan Brassow DMERR("Unable to register persistent exception store type"); 875493df71cSJonathan Brassow return r; 876493df71cSJonathan Brassow } 877493df71cSJonathan Brassow 878493df71cSJonathan Brassow r = dm_exception_store_type_register(&_persistent_compat_type); 879493df71cSJonathan Brassow if (r) { 880493df71cSJonathan Brassow DMERR("Unable to register old-style persistent exception " 881493df71cSJonathan Brassow "store type"); 882493df71cSJonathan Brassow dm_exception_store_type_unregister(&_persistent_type); 883493df71cSJonathan Brassow return r; 884493df71cSJonathan Brassow } 885493df71cSJonathan Brassow 886493df71cSJonathan Brassow return r; 8874db6bfe0SAlasdair G Kergon } 8884db6bfe0SAlasdair G Kergon 8894db6bfe0SAlasdair G Kergon void dm_persistent_snapshot_exit(void) 8904db6bfe0SAlasdair G Kergon { 891493df71cSJonathan Brassow dm_exception_store_type_unregister(&_persistent_type); 892493df71cSJonathan Brassow dm_exception_store_type_unregister(&_persistent_compat_type); 8934db6bfe0SAlasdair G Kergon } 894