blob: 92812365702438c063746413f137bc4bfe8abc15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3 *
4 * This file is released under the GPL.
5 */
6
7#ifndef DM_SNAPSHOT_H
8#define DM_SNAPSHOT_H
9
Mikulas Patocka586e80e2008-10-21 17:44:59 +010010#include <linux/device-mapper.h>
Jonathan Brassowaea53d92009-01-06 03:05:15 +000011#include "dm-exception-store.h"
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070012#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/blkdev.h>
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070014#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16struct exception_table {
17 uint32_t hash_mask;
Milan Brozd74f81f2008-02-08 02:11:27 +000018 unsigned hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 struct list_head *table;
20};
21
Mikulas Patockacd45daf2008-07-21 12:00:32 +010022#define DM_TRACKED_CHUNK_HASH_SIZE 16
23#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
24 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct dm_snapshot {
27 struct rw_semaphore lock;
Mikulas Patocka72727ba2008-04-24 21:43:11 +010028 struct dm_target *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 struct dm_dev *origin;
31 struct dm_dev *cow;
32
33 /* List of snapshots per Origin */
34 struct list_head list;
35
36 /* Size of data blocks saved - must be a power of 2 */
37 chunk_t chunk_size;
38 chunk_t chunk_mask;
39 chunk_t chunk_shift;
40
41 /* You can't use a snapshot if this is 0 (e.g. if full) */
42 int valid;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -080043
44 /* Origin writes don't trigger exceptions until this is set */
45 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 /* Used for display of table */
48 char type;
49
Mikulas Patocka92e86812008-07-21 12:00:35 +010050 mempool_t *pending_pool;
51
Mikulas Patocka879129d2008-10-30 13:33:16 +000052 atomic_t pending_exceptions_count;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct exception_table pending;
55 struct exception_table complete;
56
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070057 /*
58 * pe_lock protects all pending_exception operations and access
59 * as well as the snapshot_bios list.
60 */
61 spinlock_t pe_lock;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /* The on disk metadata handler */
Jonathan Brassow1ae25f92009-01-06 03:05:16 +000064 struct dm_exception_store store;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +010066 struct dm_kcopyd_client *kcopyd_client;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070067
68 /* Queue of snapshot writes for ksnapd to flush */
69 struct bio_list queued_bios;
70 struct work_struct queued_bios_work;
Mikulas Patockacd45daf2008-07-21 12:00:32 +010071
72 /* Chunks with outstanding reads */
73 mempool_t *tracked_chunk_pool;
74 spinlock_t tracked_chunk_lock;
75 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
78/*
79 * Used by the exception stores to load exceptions hen
80 * initialising.
81 */
82int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new);
83
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Return the number of sectors in the device.
86 */
87static inline sector_t get_dev_size(struct block_device *bdev)
88{
89 return bdev->bd_inode->i_size >> SECTOR_SHIFT;
90}
91
92static inline chunk_t sector_to_chunk(struct dm_snapshot *s, sector_t sector)
93{
94 return (sector & ~s->chunk_mask) >> s->chunk_shift;
95}
96
97static inline sector_t chunk_to_sector(struct dm_snapshot *s, chunk_t chunk)
98{
99 return chunk << s->chunk_shift;
100}
101
102static inline int bdev_equal(struct block_device *lhs, struct block_device *rhs)
103{
104 /*
105 * There is only ever one instance of a particular block
106 * device so we can compare pointers safely.
107 */
108 return lhs == rhs;
109}
110
111#endif