staging/rts_pstor: remove braces {} in sd.c (sd_pull_ctl_enable)
[linux-2.6/btrfs-unstable.git] / drivers / md / persistent-data / dm-transaction-manager.c
blobd247a35da3c63030bca417b89482bb8fdf371cd0
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
6 #include "dm-transaction-manager.h"
7 #include "dm-space-map.h"
8 #include "dm-space-map-disk.h"
9 #include "dm-space-map-metadata.h"
10 #include "dm-persistent-data-internal.h"
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/device-mapper.h>
16 #define DM_MSG_PREFIX "transaction manager"
18 /*----------------------------------------------------------------*/
20 struct shadow_info {
21 struct hlist_node hlist;
22 dm_block_t where;
26 * It would be nice if we scaled with the size of transaction.
28 #define HASH_SIZE 256
29 #define HASH_MASK (HASH_SIZE - 1)
31 struct dm_transaction_manager {
32 int is_clone;
33 struct dm_transaction_manager *real;
35 struct dm_block_manager *bm;
36 struct dm_space_map *sm;
38 spinlock_t lock;
39 struct hlist_head buckets[HASH_SIZE];
42 /*----------------------------------------------------------------*/
44 static int is_shadow(struct dm_transaction_manager *tm, dm_block_t b)
46 int r = 0;
47 unsigned bucket = dm_hash_block(b, HASH_MASK);
48 struct shadow_info *si;
49 struct hlist_node *n;
51 spin_lock(&tm->lock);
52 hlist_for_each_entry(si, n, tm->buckets + bucket, hlist)
53 if (si->where == b) {
54 r = 1;
55 break;
57 spin_unlock(&tm->lock);
59 return r;
63 * This can silently fail if there's no memory. We're ok with this since
64 * creating redundant shadows causes no harm.
66 static void insert_shadow(struct dm_transaction_manager *tm, dm_block_t b)
68 unsigned bucket;
69 struct shadow_info *si;
71 si = kmalloc(sizeof(*si), GFP_NOIO);
72 if (si) {
73 si->where = b;
74 bucket = dm_hash_block(b, HASH_MASK);
75 spin_lock(&tm->lock);
76 hlist_add_head(&si->hlist, tm->buckets + bucket);
77 spin_unlock(&tm->lock);
81 static void wipe_shadow_table(struct dm_transaction_manager *tm)
83 struct shadow_info *si;
84 struct hlist_node *n, *tmp;
85 struct hlist_head *bucket;
86 int i;
88 spin_lock(&tm->lock);
89 for (i = 0; i < HASH_SIZE; i++) {
90 bucket = tm->buckets + i;
91 hlist_for_each_entry_safe(si, n, tmp, bucket, hlist)
92 kfree(si);
94 INIT_HLIST_HEAD(bucket);
97 spin_unlock(&tm->lock);
100 /*----------------------------------------------------------------*/
102 static struct dm_transaction_manager *dm_tm_create(struct dm_block_manager *bm,
103 struct dm_space_map *sm)
105 int i;
106 struct dm_transaction_manager *tm;
108 tm = kmalloc(sizeof(*tm), GFP_KERNEL);
109 if (!tm)
110 return ERR_PTR(-ENOMEM);
112 tm->is_clone = 0;
113 tm->real = NULL;
114 tm->bm = bm;
115 tm->sm = sm;
117 spin_lock_init(&tm->lock);
118 for (i = 0; i < HASH_SIZE; i++)
119 INIT_HLIST_HEAD(tm->buckets + i);
121 return tm;
124 struct dm_transaction_manager *dm_tm_create_non_blocking_clone(struct dm_transaction_manager *real)
126 struct dm_transaction_manager *tm;
128 tm = kmalloc(sizeof(*tm), GFP_KERNEL);
129 if (tm) {
130 tm->is_clone = 1;
131 tm->real = real;
134 return tm;
136 EXPORT_SYMBOL_GPL(dm_tm_create_non_blocking_clone);
138 void dm_tm_destroy(struct dm_transaction_manager *tm)
140 if (!tm->is_clone)
141 wipe_shadow_table(tm);
143 kfree(tm);
145 EXPORT_SYMBOL_GPL(dm_tm_destroy);
147 int dm_tm_pre_commit(struct dm_transaction_manager *tm)
149 int r;
151 if (tm->is_clone)
152 return -EWOULDBLOCK;
154 r = dm_sm_commit(tm->sm);
155 if (r < 0)
156 return r;
158 return 0;
160 EXPORT_SYMBOL_GPL(dm_tm_pre_commit);
162 int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *root)
164 if (tm->is_clone)
165 return -EWOULDBLOCK;
167 wipe_shadow_table(tm);
169 return dm_bm_flush_and_unlock(tm->bm, root);
171 EXPORT_SYMBOL_GPL(dm_tm_commit);
173 int dm_tm_new_block(struct dm_transaction_manager *tm,
174 struct dm_block_validator *v,
175 struct dm_block **result)
177 int r;
178 dm_block_t new_block;
180 if (tm->is_clone)
181 return -EWOULDBLOCK;
183 r = dm_sm_new_block(tm->sm, &new_block);
184 if (r < 0)
185 return r;
187 r = dm_bm_write_lock_zero(tm->bm, new_block, v, result);
188 if (r < 0) {
189 dm_sm_dec_block(tm->sm, new_block);
190 return r;
194 * New blocks count as shadows in that they don't need to be
195 * shadowed again.
197 insert_shadow(tm, new_block);
199 return 0;
202 static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
203 struct dm_block_validator *v,
204 struct dm_block **result)
206 int r;
207 dm_block_t new;
208 struct dm_block *orig_block;
210 r = dm_sm_new_block(tm->sm, &new);
211 if (r < 0)
212 return r;
214 r = dm_sm_dec_block(tm->sm, orig);
215 if (r < 0)
216 return r;
218 r = dm_bm_read_lock(tm->bm, orig, v, &orig_block);
219 if (r < 0)
220 return r;
223 * It would be tempting to use dm_bm_unlock_move here, but some
224 * code, such as the space maps, keeps using the old data structures
225 * secure in the knowledge they won't be changed until the next
226 * transaction. Using unlock_move would force a synchronous read
227 * since the old block would no longer be in the cache.
229 r = dm_bm_write_lock_zero(tm->bm, new, v, result);
230 if (r) {
231 dm_bm_unlock(orig_block);
232 return r;
235 memcpy(dm_block_data(*result), dm_block_data(orig_block),
236 dm_bm_block_size(tm->bm));
238 dm_bm_unlock(orig_block);
239 return r;
242 int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
243 struct dm_block_validator *v, struct dm_block **result,
244 int *inc_children)
246 int r;
248 if (tm->is_clone)
249 return -EWOULDBLOCK;
251 r = dm_sm_count_is_more_than_one(tm->sm, orig, inc_children);
252 if (r < 0)
253 return r;
255 if (is_shadow(tm, orig) && !*inc_children)
256 return dm_bm_write_lock(tm->bm, orig, v, result);
258 r = __shadow_block(tm, orig, v, result);
259 if (r < 0)
260 return r;
261 insert_shadow(tm, dm_block_location(*result));
263 return r;
265 EXPORT_SYMBOL_GPL(dm_tm_shadow_block);
267 int dm_tm_read_lock(struct dm_transaction_manager *tm, dm_block_t b,
268 struct dm_block_validator *v,
269 struct dm_block **blk)
271 if (tm->is_clone)
272 return dm_bm_read_try_lock(tm->real->bm, b, v, blk);
274 return dm_bm_read_lock(tm->bm, b, v, blk);
276 EXPORT_SYMBOL_GPL(dm_tm_read_lock);
278 int dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b)
280 return dm_bm_unlock(b);
282 EXPORT_SYMBOL_GPL(dm_tm_unlock);
284 void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b)
287 * The non-blocking clone doesn't support this.
289 BUG_ON(tm->is_clone);
291 dm_sm_inc_block(tm->sm, b);
293 EXPORT_SYMBOL_GPL(dm_tm_inc);
295 void dm_tm_dec(struct dm_transaction_manager *tm, dm_block_t b)
298 * The non-blocking clone doesn't support this.
300 BUG_ON(tm->is_clone);
302 dm_sm_dec_block(tm->sm, b);
304 EXPORT_SYMBOL_GPL(dm_tm_dec);
306 int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b,
307 uint32_t *result)
309 if (tm->is_clone)
310 return -EWOULDBLOCK;
312 return dm_sm_get_count(tm->sm, b, result);
315 struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm)
317 return tm->bm;
320 /*----------------------------------------------------------------*/
322 static int dm_tm_create_internal(struct dm_block_manager *bm,
323 dm_block_t sb_location,
324 struct dm_transaction_manager **tm,
325 struct dm_space_map **sm,
326 int create,
327 void *sm_root, size_t sm_len)
329 int r;
331 *sm = dm_sm_metadata_init();
332 if (IS_ERR(*sm))
333 return PTR_ERR(*sm);
335 *tm = dm_tm_create(bm, *sm);
336 if (IS_ERR(*tm)) {
337 dm_sm_destroy(*sm);
338 return PTR_ERR(*tm);
341 if (create) {
342 r = dm_sm_metadata_create(*sm, *tm, dm_bm_nr_blocks(bm),
343 sb_location);
344 if (r) {
345 DMERR("couldn't create metadata space map");
346 goto bad;
349 } else {
350 r = dm_sm_metadata_open(*sm, *tm, sm_root, sm_len);
351 if (r) {
352 DMERR("couldn't open metadata space map");
353 goto bad;
357 return 0;
359 bad:
360 dm_tm_destroy(*tm);
361 dm_sm_destroy(*sm);
362 return r;
365 int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
366 struct dm_transaction_manager **tm,
367 struct dm_space_map **sm)
369 return dm_tm_create_internal(bm, sb_location, tm, sm, 1, NULL, 0);
371 EXPORT_SYMBOL_GPL(dm_tm_create_with_sm);
373 int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
374 void *sm_root, size_t root_len,
375 struct dm_transaction_manager **tm,
376 struct dm_space_map **sm)
378 return dm_tm_create_internal(bm, sb_location, tm, sm, 0, sm_root, root_len);
380 EXPORT_SYMBOL_GPL(dm_tm_open_with_sm);
382 /*----------------------------------------------------------------*/