Blame view

drivers/md/dm-snap-transient.c 3.66 KB
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
1
2
3
4
5
6
7
8
  /*
   * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
   * Copyright (C) 2006-2008 Red Hat GmbH
   *
   * This file is released under the GPL.
   */
  
  #include "dm-exception-store.h"
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
9
10
11
12
  
  #include <linux/mm.h>
  #include <linux/pagemap.h>
  #include <linux/vmalloc.h>
daaa5f7cb   Paul Gortmaker   md: Add in export...
13
  #include <linux/export.h>
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
14
15
16
17
18
19
20
21
22
23
24
  #include <linux/slab.h>
  #include <linux/dm-io.h>
  
  #define DM_MSG_PREFIX "transient snapshot"
  
  /*-----------------------------------------------------------------
   * Implementation of the store for non-persistent snapshots.
   *---------------------------------------------------------------*/
  struct transient_c {
  	sector_t next_free;
  };
493df71c6   Jonathan Brassow   dm exception stor...
25
  static void transient_dtr(struct dm_exception_store *store)
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
26
27
28
  {
  	kfree(store->context);
  }
a159c1ac5   Jonathan Brassow   dm snapshot: exte...
29
30
31
32
  static int transient_read_metadata(struct dm_exception_store *store,
  				   int (*callback)(void *callback_context,
  						   chunk_t old, chunk_t new),
  				   void *callback_context)
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
33
34
35
  {
  	return 0;
  }
a159c1ac5   Jonathan Brassow   dm snapshot: exte...
36
  static int transient_prepare_exception(struct dm_exception_store *store,
1d4989c85   Jon Brassow   dm snapshot: rena...
37
  				       struct dm_exception *e)
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
38
  {
b2a114652   Jonathan Brassow   dm exception stor...
39
  	struct transient_c *tc = store->context;
fc56f6fbc   Mike Snitzer   dm snapshot: move...
40
  	sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
41

d02168495   Jonathan Brassow   dm exception stor...
42
  	if (size < (tc->next_free + store->chunk_size))
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
43
  		return -1;
71fab00a6   Jonathan Brassow   dm snapshot: remo...
44
  	e->new_chunk = sector_to_chunk(store, tc->next_free);
d02168495   Jonathan Brassow   dm exception stor...
45
  	tc->next_free += store->chunk_size;
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
46
47
48
  
  	return 0;
  }
a159c1ac5   Jonathan Brassow   dm snapshot: exte...
49
  static void transient_commit_exception(struct dm_exception_store *store,
1d4989c85   Jon Brassow   dm snapshot: rena...
50
  				       struct dm_exception *e,
a159c1ac5   Jonathan Brassow   dm snapshot: exte...
51
52
  				       void (*callback) (void *, int success),
  				       void *callback_context)
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
53
54
55
56
  {
  	/* Just succeed */
  	callback(callback_context, 1);
  }
985903bb3   Mike Snitzer   dm snapshot: add ...
57
58
59
60
  static void transient_usage(struct dm_exception_store *store,
  			    sector_t *total_sectors,
  			    sector_t *sectors_allocated,
  			    sector_t *metadata_sectors)
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
61
  {
985903bb3   Mike Snitzer   dm snapshot: add ...
62
  	*sectors_allocated = ((struct transient_c *) store->context)->next_free;
fc56f6fbc   Mike Snitzer   dm snapshot: move...
63
  	*total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
985903bb3   Mike Snitzer   dm snapshot: add ...
64
  	*metadata_sectors = 0;
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
65
  }
493df71c6   Jonathan Brassow   dm exception stor...
66
67
  static int transient_ctr(struct dm_exception_store *store,
  			 unsigned argc, char **argv)
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
68
69
  {
  	struct transient_c *tc;
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
70
71
72
73
74
75
76
77
78
  	tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  	if (!tc)
  		return -ENOMEM;
  
  	tc->next_free = 0;
  	store->context = tc;
  
  	return 0;
  }
1e302a929   Jonathan Brassow   dm snapshot: move...
79
80
81
  static unsigned transient_status(struct dm_exception_store *store,
  				 status_type_t status, char *result,
  				 unsigned maxlen)
493df71c6   Jonathan Brassow   dm exception stor...
82
  {
1e302a929   Jonathan Brassow   dm snapshot: move...
83
84
85
86
87
88
  	unsigned sz = 0;
  
  	switch (status) {
  	case STATUSTYPE_INFO:
  		break;
  	case STATUSTYPE_TABLE:
fc56f6fbc   Mike Snitzer   dm snapshot: move...
89
  		DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
1e302a929   Jonathan Brassow   dm snapshot: move...
90
  	}
493df71c6   Jonathan Brassow   dm exception stor...
91
92
93
94
95
96
97
98
99
100
101
102
  
  	return sz;
  }
  
  static struct dm_exception_store_type _transient_type = {
  	.name = "transient",
  	.module = THIS_MODULE,
  	.ctr = transient_ctr,
  	.dtr = transient_dtr,
  	.read_metadata = transient_read_metadata,
  	.prepare_exception = transient_prepare_exception,
  	.commit_exception = transient_commit_exception,
985903bb3   Mike Snitzer   dm snapshot: add ...
103
  	.usage = transient_usage,
493df71c6   Jonathan Brassow   dm exception stor...
104
105
106
107
108
109
110
111
112
113
114
  	.status = transient_status,
  };
  
  static struct dm_exception_store_type _transient_compat_type = {
  	.name = "N",
  	.module = THIS_MODULE,
  	.ctr = transient_ctr,
  	.dtr = transient_dtr,
  	.read_metadata = transient_read_metadata,
  	.prepare_exception = transient_prepare_exception,
  	.commit_exception = transient_commit_exception,
985903bb3   Mike Snitzer   dm snapshot: add ...
115
  	.usage = transient_usage,
493df71c6   Jonathan Brassow   dm exception stor...
116
117
  	.status = transient_status,
  };
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
118
119
  int dm_transient_snapshot_init(void)
  {
493df71c6   Jonathan Brassow   dm exception stor...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  	int r;
  
  	r = dm_exception_store_type_register(&_transient_type);
  	if (r) {
  		DMWARN("Unable to register transient exception store type");
  		return r;
  	}
  
  	r = dm_exception_store_type_register(&_transient_compat_type);
  	if (r) {
  		DMWARN("Unable to register old-style transient "
  		       "exception store type");
  		dm_exception_store_type_unregister(&_transient_type);
  		return r;
  	}
  
  	return r;
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
137
138
139
140
  }
  
  void dm_transient_snapshot_exit(void)
  {
493df71c6   Jonathan Brassow   dm exception stor...
141
142
  	dm_exception_store_type_unregister(&_transient_type);
  	dm_exception_store_type_unregister(&_transient_compat_type);
4db6bfe02   Alasdair G Kergon   dm snapshot: spli...
143
  }