2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
6 #include "dm-transaction-manager.h"
7 #include "dm-space-map.h"
8 #include "dm-space-map-checker.h"
9 #include "dm-space-map-disk.h"
10 #include "dm-space-map-metadata.h"
11 #include "dm-persistent-data-internal.h"
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/device-mapper.h>
17 #define DM_MSG_PREFIX "transaction manager"
19 /*----------------------------------------------------------------*/
22 struct hlist_node hlist
;
27 * It would be nice if we scaled with the size of transaction.
30 #define HASH_MASK (HASH_SIZE - 1)
32 struct dm_transaction_manager
{
34 struct dm_transaction_manager
*real
;
36 struct dm_block_manager
*bm
;
37 struct dm_space_map
*sm
;
40 struct hlist_head buckets
[HASH_SIZE
];
43 /*----------------------------------------------------------------*/
45 static int is_shadow(struct dm_transaction_manager
*tm
, dm_block_t b
)
48 unsigned bucket
= dm_hash_block(b
, HASH_MASK
);
49 struct shadow_info
*si
;
53 hlist_for_each_entry(si
, n
, tm
->buckets
+ bucket
, hlist
)
58 spin_unlock(&tm
->lock
);
64 * This can silently fail if there's no memory. We're ok with this since
65 * creating redundant shadows causes no harm.
67 static void insert_shadow(struct dm_transaction_manager
*tm
, dm_block_t b
)
70 struct shadow_info
*si
;
72 si
= kmalloc(sizeof(*si
), GFP_NOIO
);
75 bucket
= dm_hash_block(b
, HASH_MASK
);
77 hlist_add_head(&si
->hlist
, tm
->buckets
+ bucket
);
78 spin_unlock(&tm
->lock
);
82 static void wipe_shadow_table(struct dm_transaction_manager
*tm
)
84 struct shadow_info
*si
;
85 struct hlist_node
*n
, *tmp
;
86 struct hlist_head
*bucket
;
90 for (i
= 0; i
< HASH_SIZE
; i
++) {
91 bucket
= tm
->buckets
+ i
;
92 hlist_for_each_entry_safe(si
, n
, tmp
, bucket
, hlist
)
95 INIT_HLIST_HEAD(bucket
);
98 spin_unlock(&tm
->lock
);
101 /*----------------------------------------------------------------*/
103 static struct dm_transaction_manager
*dm_tm_create(struct dm_block_manager
*bm
,
104 struct dm_space_map
*sm
)
107 struct dm_transaction_manager
*tm
;
109 tm
= kmalloc(sizeof(*tm
), GFP_KERNEL
);
111 return ERR_PTR(-ENOMEM
);
118 spin_lock_init(&tm
->lock
);
119 for (i
= 0; i
< HASH_SIZE
; i
++)
120 INIT_HLIST_HEAD(tm
->buckets
+ i
);
125 struct dm_transaction_manager
*dm_tm_create_non_blocking_clone(struct dm_transaction_manager
*real
)
127 struct dm_transaction_manager
*tm
;
129 tm
= kmalloc(sizeof(*tm
), GFP_KERNEL
);
137 EXPORT_SYMBOL_GPL(dm_tm_create_non_blocking_clone
);
139 void dm_tm_destroy(struct dm_transaction_manager
*tm
)
142 wipe_shadow_table(tm
);
146 EXPORT_SYMBOL_GPL(dm_tm_destroy
);
148 int dm_tm_pre_commit(struct dm_transaction_manager
*tm
)
155 r
= dm_sm_commit(tm
->sm
);
161 EXPORT_SYMBOL_GPL(dm_tm_pre_commit
);
163 int dm_tm_commit(struct dm_transaction_manager
*tm
, struct dm_block
*root
)
168 wipe_shadow_table(tm
);
170 return dm_bm_flush_and_unlock(tm
->bm
, root
);
172 EXPORT_SYMBOL_GPL(dm_tm_commit
);
174 int dm_tm_new_block(struct dm_transaction_manager
*tm
,
175 struct dm_block_validator
*v
,
176 struct dm_block
**result
)
179 dm_block_t new_block
;
184 r
= dm_sm_new_block(tm
->sm
, &new_block
);
188 r
= dm_bm_write_lock_zero(tm
->bm
, new_block
, v
, result
);
190 dm_sm_dec_block(tm
->sm
, new_block
);
195 * New blocks count as shadows in that they don't need to be
198 insert_shadow(tm
, new_block
);
203 static int __shadow_block(struct dm_transaction_manager
*tm
, dm_block_t orig
,
204 struct dm_block_validator
*v
,
205 struct dm_block
**result
)
209 struct dm_block
*orig_block
;
211 r
= dm_sm_new_block(tm
->sm
, &new);
215 r
= dm_sm_dec_block(tm
->sm
, orig
);
219 r
= dm_bm_read_lock(tm
->bm
, orig
, v
, &orig_block
);
223 r
= dm_bm_unlock_move(orig_block
, new);
225 dm_bm_unlock(orig_block
);
229 return dm_bm_write_lock(tm
->bm
, new, v
, result
);
232 int dm_tm_shadow_block(struct dm_transaction_manager
*tm
, dm_block_t orig
,
233 struct dm_block_validator
*v
, struct dm_block
**result
,
241 r
= dm_sm_count_is_more_than_one(tm
->sm
, orig
, inc_children
);
245 if (is_shadow(tm
, orig
) && !*inc_children
)
246 return dm_bm_write_lock(tm
->bm
, orig
, v
, result
);
248 r
= __shadow_block(tm
, orig
, v
, result
);
251 insert_shadow(tm
, dm_block_location(*result
));
256 int dm_tm_read_lock(struct dm_transaction_manager
*tm
, dm_block_t b
,
257 struct dm_block_validator
*v
,
258 struct dm_block
**blk
)
261 return dm_bm_read_try_lock(tm
->real
->bm
, b
, v
, blk
);
263 return dm_bm_read_lock(tm
->bm
, b
, v
, blk
);
266 int dm_tm_unlock(struct dm_transaction_manager
*tm
, struct dm_block
*b
)
268 return dm_bm_unlock(b
);
270 EXPORT_SYMBOL_GPL(dm_tm_unlock
);
272 void dm_tm_inc(struct dm_transaction_manager
*tm
, dm_block_t b
)
275 * The non-blocking clone doesn't support this.
277 BUG_ON(tm
->is_clone
);
279 dm_sm_inc_block(tm
->sm
, b
);
281 EXPORT_SYMBOL_GPL(dm_tm_inc
);
283 void dm_tm_dec(struct dm_transaction_manager
*tm
, dm_block_t b
)
286 * The non-blocking clone doesn't support this.
288 BUG_ON(tm
->is_clone
);
290 dm_sm_dec_block(tm
->sm
, b
);
292 EXPORT_SYMBOL_GPL(dm_tm_dec
);
294 int dm_tm_ref(struct dm_transaction_manager
*tm
, dm_block_t b
,
300 return dm_sm_get_count(tm
->sm
, b
, result
);
303 struct dm_block_manager
*dm_tm_get_bm(struct dm_transaction_manager
*tm
)
308 /*----------------------------------------------------------------*/
310 static int dm_tm_create_internal(struct dm_block_manager
*bm
,
311 dm_block_t sb_location
,
312 struct dm_block_validator
*sb_validator
,
313 size_t root_offset
, size_t root_max_len
,
314 struct dm_transaction_manager
**tm
,
315 struct dm_space_map
**sm
,
316 struct dm_block
**sblock
,
320 struct dm_space_map
*inner
;
322 inner
= dm_sm_metadata_init();
324 return PTR_ERR(inner
);
326 *tm
= dm_tm_create(bm
, inner
);
328 dm_sm_destroy(inner
);
333 r
= dm_bm_write_lock_zero(dm_tm_get_bm(*tm
), sb_location
,
334 sb_validator
, sblock
);
336 DMERR("couldn't lock superblock");
340 r
= dm_sm_metadata_create(inner
, *tm
, dm_bm_nr_blocks(bm
),
343 DMERR("couldn't create metadata space map");
347 *sm
= dm_sm_checker_create(inner
);
354 r
= dm_bm_write_lock(dm_tm_get_bm(*tm
), sb_location
,
355 sb_validator
, sblock
);
357 DMERR("couldn't lock superblock");
361 r
= dm_sm_metadata_open(inner
, *tm
,
362 dm_block_data(*sblock
) + root_offset
,
365 DMERR("couldn't open metadata space map");
369 *sm
= dm_sm_checker_create(inner
);
379 dm_tm_unlock(*tm
, *sblock
);
382 dm_sm_destroy(inner
);
386 int dm_tm_create_with_sm(struct dm_block_manager
*bm
, dm_block_t sb_location
,
387 struct dm_block_validator
*sb_validator
,
388 struct dm_transaction_manager
**tm
,
389 struct dm_space_map
**sm
, struct dm_block
**sblock
)
391 return dm_tm_create_internal(bm
, sb_location
, sb_validator
,
392 0, 0, tm
, sm
, sblock
, 1);
394 EXPORT_SYMBOL_GPL(dm_tm_create_with_sm
);
396 int dm_tm_open_with_sm(struct dm_block_manager
*bm
, dm_block_t sb_location
,
397 struct dm_block_validator
*sb_validator
,
398 size_t root_offset
, size_t root_max_len
,
399 struct dm_transaction_manager
**tm
,
400 struct dm_space_map
**sm
, struct dm_block
**sblock
)
402 return dm_tm_create_internal(bm
, sb_location
, sb_validator
, root_offset
,
403 root_max_len
, tm
, sm
, sblock
, 0);
405 EXPORT_SYMBOL_GPL(dm_tm_open_with_sm
);
407 /*----------------------------------------------------------------*/