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 114db6bfe0SAlasdair G Kergon #include <linux/mm.h> 124db6bfe0SAlasdair G Kergon #include <linux/pagemap.h> 134db6bfe0SAlasdair G Kergon #include <linux/vmalloc.h> 14daaa5f7cSPaul Gortmaker #include <linux/export.h> 154db6bfe0SAlasdair G Kergon #include <linux/slab.h> 164db6bfe0SAlasdair G Kergon #include <linux/dm-io.h> 174db6bfe0SAlasdair G Kergon 184db6bfe0SAlasdair G Kergon #define DM_MSG_PREFIX "transient snapshot" 194db6bfe0SAlasdair G Kergon 20*a4a82ce3SHeinz Mauelshagen /* 21*a4a82ce3SHeinz Mauelshagen *--------------------------------------------------------------- 224db6bfe0SAlasdair G Kergon * Implementation of the store for non-persistent snapshots. 23*a4a82ce3SHeinz Mauelshagen *--------------------------------------------------------------- 24*a4a82ce3SHeinz Mauelshagen */ 254db6bfe0SAlasdair G Kergon struct transient_c { 264db6bfe0SAlasdair G Kergon sector_t next_free; 274db6bfe0SAlasdair G Kergon }; 284db6bfe0SAlasdair G Kergon 29493df71cSJonathan Brassow static void transient_dtr(struct dm_exception_store *store) 304db6bfe0SAlasdair G Kergon { 314db6bfe0SAlasdair G Kergon kfree(store->context); 324db6bfe0SAlasdair G Kergon } 334db6bfe0SAlasdair G Kergon 34a159c1acSJonathan Brassow static int transient_read_metadata(struct dm_exception_store *store, 35a159c1acSJonathan Brassow int (*callback)(void *callback_context, 36a159c1acSJonathan Brassow chunk_t old, chunk_t new), 37a159c1acSJonathan Brassow void *callback_context) 384db6bfe0SAlasdair G Kergon { 394db6bfe0SAlasdair G Kergon return 0; 404db6bfe0SAlasdair G Kergon } 414db6bfe0SAlasdair G Kergon 42a159c1acSJonathan Brassow static int transient_prepare_exception(struct dm_exception_store *store, 431d4989c8SJon Brassow struct dm_exception *e) 444db6bfe0SAlasdair G Kergon { 45b2a11465SJonathan Brassow struct transient_c *tc = store->context; 46fc56f6fbSMike Snitzer sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev); 474db6bfe0SAlasdair G Kergon 48d0216849SJonathan Brassow if (size < (tc->next_free + store->chunk_size)) 494db6bfe0SAlasdair G Kergon return -1; 504db6bfe0SAlasdair G Kergon 5171fab00aSJonathan Brassow e->new_chunk = sector_to_chunk(store, tc->next_free); 52d0216849SJonathan Brassow tc->next_free += store->chunk_size; 534db6bfe0SAlasdair G Kergon 544db6bfe0SAlasdair G Kergon return 0; 554db6bfe0SAlasdair G Kergon } 564db6bfe0SAlasdair G Kergon 57a159c1acSJonathan Brassow static void transient_commit_exception(struct dm_exception_store *store, 58385277bfSMikulas Patocka struct dm_exception *e, int valid, 594db6bfe0SAlasdair G Kergon void (*callback) (void *, int success), 604db6bfe0SAlasdair G Kergon void *callback_context) 614db6bfe0SAlasdair G Kergon { 624db6bfe0SAlasdair G Kergon /* Just succeed */ 63385277bfSMikulas Patocka callback(callback_context, valid); 644db6bfe0SAlasdair G Kergon } 654db6bfe0SAlasdair G Kergon 66985903bbSMike Snitzer static void transient_usage(struct dm_exception_store *store, 67985903bbSMike Snitzer sector_t *total_sectors, 68985903bbSMike Snitzer sector_t *sectors_allocated, 69985903bbSMike Snitzer sector_t *metadata_sectors) 704db6bfe0SAlasdair G Kergon { 71985903bbSMike Snitzer *sectors_allocated = ((struct transient_c *) store->context)->next_free; 72fc56f6fbSMike Snitzer *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev); 73985903bbSMike Snitzer *metadata_sectors = 0; 744db6bfe0SAlasdair G Kergon } 754db6bfe0SAlasdair G Kergon 76b0d3cc01SMike Snitzer static int transient_ctr(struct dm_exception_store *store, char *options) 774db6bfe0SAlasdair G Kergon { 784db6bfe0SAlasdair G Kergon struct transient_c *tc; 794db6bfe0SAlasdair G Kergon 804db6bfe0SAlasdair G Kergon tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL); 814db6bfe0SAlasdair G Kergon if (!tc) 824db6bfe0SAlasdair G Kergon return -ENOMEM; 834db6bfe0SAlasdair G Kergon 844db6bfe0SAlasdair G Kergon tc->next_free = 0; 854db6bfe0SAlasdair G Kergon store->context = tc; 864db6bfe0SAlasdair G Kergon 874db6bfe0SAlasdair G Kergon return 0; 884db6bfe0SAlasdair G Kergon } 894db6bfe0SAlasdair G Kergon 9086a3238cSHeinz Mauelshagen static unsigned int transient_status(struct dm_exception_store *store, 91493df71cSJonathan Brassow status_type_t status, char *result, 9286a3238cSHeinz Mauelshagen unsigned int maxlen) 93493df71cSJonathan Brassow { 9486a3238cSHeinz Mauelshagen unsigned int sz = 0; 951e302a92SJonathan Brassow 961e302a92SJonathan Brassow switch (status) { 971e302a92SJonathan Brassow case STATUSTYPE_INFO: 981e302a92SJonathan Brassow break; 991e302a92SJonathan Brassow case STATUSTYPE_TABLE: 100fc56f6fbSMike Snitzer DMEMIT(" N %llu", (unsigned long long)store->chunk_size); 1018ec45662STushar Sugandhi break; 1028ec45662STushar Sugandhi case STATUSTYPE_IMA: 1038ec45662STushar Sugandhi *result = '\0'; 1048ec45662STushar Sugandhi break; 1051e302a92SJonathan Brassow } 106493df71cSJonathan Brassow 107493df71cSJonathan Brassow return sz; 108493df71cSJonathan Brassow } 109493df71cSJonathan Brassow 110493df71cSJonathan Brassow static struct dm_exception_store_type _transient_type = { 111493df71cSJonathan Brassow .name = "transient", 112493df71cSJonathan Brassow .module = THIS_MODULE, 113493df71cSJonathan Brassow .ctr = transient_ctr, 114493df71cSJonathan Brassow .dtr = transient_dtr, 115493df71cSJonathan Brassow .read_metadata = transient_read_metadata, 116493df71cSJonathan Brassow .prepare_exception = transient_prepare_exception, 117493df71cSJonathan Brassow .commit_exception = transient_commit_exception, 118985903bbSMike Snitzer .usage = transient_usage, 119493df71cSJonathan Brassow .status = transient_status, 120493df71cSJonathan Brassow }; 121493df71cSJonathan Brassow 122493df71cSJonathan Brassow static struct dm_exception_store_type _transient_compat_type = { 123493df71cSJonathan Brassow .name = "N", 124493df71cSJonathan Brassow .module = THIS_MODULE, 125493df71cSJonathan Brassow .ctr = transient_ctr, 126493df71cSJonathan Brassow .dtr = transient_dtr, 127493df71cSJonathan Brassow .read_metadata = transient_read_metadata, 128493df71cSJonathan Brassow .prepare_exception = transient_prepare_exception, 129493df71cSJonathan Brassow .commit_exception = transient_commit_exception, 130985903bbSMike Snitzer .usage = transient_usage, 131493df71cSJonathan Brassow .status = transient_status, 132493df71cSJonathan Brassow }; 133493df71cSJonathan Brassow 1344db6bfe0SAlasdair G Kergon int dm_transient_snapshot_init(void) 1354db6bfe0SAlasdair G Kergon { 136493df71cSJonathan Brassow int r; 137493df71cSJonathan Brassow 138493df71cSJonathan Brassow r = dm_exception_store_type_register(&_transient_type); 139493df71cSJonathan Brassow if (r) { 140493df71cSJonathan Brassow DMWARN("Unable to register transient exception store type"); 141493df71cSJonathan Brassow return r; 142493df71cSJonathan Brassow } 143493df71cSJonathan Brassow 144493df71cSJonathan Brassow r = dm_exception_store_type_register(&_transient_compat_type); 145493df71cSJonathan Brassow if (r) { 146493df71cSJonathan Brassow DMWARN("Unable to register old-style transient " 147493df71cSJonathan Brassow "exception store type"); 148493df71cSJonathan Brassow dm_exception_store_type_unregister(&_transient_type); 149493df71cSJonathan Brassow return r; 150493df71cSJonathan Brassow } 151493df71cSJonathan Brassow 152493df71cSJonathan Brassow return r; 1534db6bfe0SAlasdair G Kergon } 1544db6bfe0SAlasdair G Kergon 1554db6bfe0SAlasdair G Kergon void dm_transient_snapshot_exit(void) 1564db6bfe0SAlasdair G Kergon { 157493df71cSJonathan Brassow dm_exception_store_type_unregister(&_transient_type); 158493df71cSJonathan Brassow dm_exception_store_type_unregister(&_transient_compat_type); 1594db6bfe0SAlasdair G Kergon } 160