blob: 650e0f1f51d8a6c312a001c8443cf3bc8064d481 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dm-snapshot.c
3 *
4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
5 *
6 * This file is released under the GPL.
7 */
8
9#ifndef DM_SNAPSHOT_H
10#define DM_SNAPSHOT_H
11
12#include "dm.h"
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070013#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/blkdev.h>
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070015#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17struct exception_table {
18 uint32_t hash_mask;
19 struct list_head *table;
20};
21
22/*
23 * The snapshot code deals with largish chunks of the disk at a
24 * time. Typically 64k - 256k.
25 */
26/* FIXME: can we get away with limiting these to a uint32_t ? */
27typedef sector_t chunk_t;
28
29/*
30 * An exception is used where an old chunk of data has been
31 * replaced by a new one.
32 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010033struct dm_snap_exception {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct list_head hash_list;
35
36 chunk_t old_chunk;
37 chunk_t new_chunk;
38};
39
40/*
41 * Abstraction to handle the meta/layout of exception stores (the
42 * COW device).
43 */
44struct exception_store {
45
46 /*
47 * Destroys this object when you've finished with it.
48 */
49 void (*destroy) (struct exception_store *store);
50
51 /*
52 * The target shouldn't read the COW device until this is
53 * called.
54 */
55 int (*read_metadata) (struct exception_store *store);
56
57 /*
58 * Find somewhere to store the next exception.
59 */
60 int (*prepare_exception) (struct exception_store *store,
Alasdair G Kergon028867a2007-07-12 17:26:32 +010061 struct dm_snap_exception *e);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /*
64 * Update the metadata with this exception.
65 */
66 void (*commit_exception) (struct exception_store *store,
Alasdair G Kergon028867a2007-07-12 17:26:32 +010067 struct dm_snap_exception *e,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 void (*callback) (void *, int success),
69 void *callback_context);
70
71 /*
72 * The snapshot is invalid, note this in the metadata.
73 */
74 void (*drop_snapshot) (struct exception_store *store);
75
76 /*
77 * Return how full the snapshot is.
78 */
79 void (*fraction_full) (struct exception_store *store,
80 sector_t *numerator,
81 sector_t *denominator);
82
83 struct dm_snapshot *snap;
84 void *context;
85};
86
87struct dm_snapshot {
88 struct rw_semaphore lock;
89 struct dm_table *table;
90
91 struct dm_dev *origin;
92 struct dm_dev *cow;
93
94 /* List of snapshots per Origin */
95 struct list_head list;
96
97 /* Size of data blocks saved - must be a power of 2 */
98 chunk_t chunk_size;
99 chunk_t chunk_mask;
100 chunk_t chunk_shift;
101
102 /* You can't use a snapshot if this is 0 (e.g. if full) */
103 int valid;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800104
105 /* Origin writes don't trigger exceptions until this is set */
106 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* Used for display of table */
109 char type;
110
111 /* The last percentage we notified */
112 int last_percent;
113
114 struct exception_table pending;
115 struct exception_table complete;
116
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700117 /*
118 * pe_lock protects all pending_exception operations and access
119 * as well as the snapshot_bios list.
120 */
121 spinlock_t pe_lock;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* The on disk metadata handler */
124 struct exception_store store;
125
126 struct kcopyd_client *kcopyd_client;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700127
128 /* Queue of snapshot writes for ksnapd to flush */
129 struct bio_list queued_bios;
130 struct work_struct queued_bios_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133/*
134 * Used by the exception stores to load exceptions hen
135 * initialising.
136 */
137int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new);
138
139/*
140 * Constructor and destructor for the default persistent
141 * store.
142 */
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700143int dm_create_persistent(struct exception_store *store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700145int dm_create_transient(struct exception_store *store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*
148 * Return the number of sectors in the device.
149 */
150static inline sector_t get_dev_size(struct block_device *bdev)
151{
152 return bdev->bd_inode->i_size >> SECTOR_SHIFT;
153}
154
155static inline chunk_t sector_to_chunk(struct dm_snapshot *s, sector_t sector)
156{
157 return (sector & ~s->chunk_mask) >> s->chunk_shift;
158}
159
160static inline sector_t chunk_to_sector(struct dm_snapshot *s, chunk_t chunk)
161{
162 return chunk << s->chunk_shift;
163}
164
165static inline int bdev_equal(struct block_device *lhs, struct block_device *rhs)
166{
167 /*
168 * There is only ever one instance of a particular block
169 * device so we can compare pointers safely.
170 */
171 return lhs == rhs;
172}
173
174#endif