2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
6 #include "dm-block-manager.h"
7 #include "dm-persistent-data-internal.h"
9 #include <linux/dm-bufio.h>
10 #include <linux/crc32c.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/rwsem.h>
14 #include <linux/device-mapper.h>
15 #include <linux/stacktrace.h>
16 #include <linux/sched/task.h>
18 #define DM_MSG_PREFIX "block manager"
20 /*----------------------------------------------------------------*/
22 #ifdef CONFIG_DM_DEBUG_BLOCK_MANAGER_LOCKING
25 * This is a read/write semaphore with a couple of differences.
27 * i) There is a restriction on the number of concurrent read locks that
28 * may be held at once. This is just an implementation detail.
30 * ii) Recursive locking attempts are detected and return EINVAL. A stack
31 * trace is also emitted for the previous lock acquisition.
33 * iii) Priority is given to write locks.
39 unsigned int nr_entries
;
40 unsigned long entries
[MAX_STACK
];
46 struct list_head waiters
;
47 struct task_struct
*holders
[MAX_HOLDERS
];
49 #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
50 struct stack_store traces
[MAX_HOLDERS
];
55 struct list_head list
;
56 struct task_struct
*task
;
60 static unsigned __find_holder(struct block_lock
*lock
,
61 struct task_struct
*task
)
65 for (i
= 0; i
< MAX_HOLDERS
; i
++)
66 if (lock
->holders
[i
] == task
)
69 BUG_ON(i
== MAX_HOLDERS
);
73 /* call this *after* you increment lock->count */
74 static void __add_holder(struct block_lock
*lock
, struct task_struct
*task
)
76 unsigned h
= __find_holder(lock
, NULL
);
77 #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
78 struct stack_store
*t
;
81 get_task_struct(task
);
82 lock
->holders
[h
] = task
;
84 #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
86 t
->nr_entries
= stack_trace_save(t
->entries
, MAX_STACK
, 2);
90 /* call this *before* you decrement lock->count */
91 static void __del_holder(struct block_lock
*lock
, struct task_struct
*task
)
93 unsigned h
= __find_holder(lock
, task
);
94 lock
->holders
[h
] = NULL
;
95 put_task_struct(task
);
98 static int __check_holder(struct block_lock
*lock
)
102 for (i
= 0; i
< MAX_HOLDERS
; i
++) {
103 if (lock
->holders
[i
] == current
) {
104 DMERR("recursive lock detected in metadata");
105 #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
106 DMERR("previously held here:");
107 stack_trace_print(lock
->traces
[i
].entries
,
108 lock
->traces
[i
].nr_entries
, 4);
110 DMERR("subsequent acquisition attempted here:");
120 static void __wait(struct waiter
*w
)
123 set_current_state(TASK_UNINTERRUPTIBLE
);
131 set_current_state(TASK_RUNNING
);
134 static void __wake_waiter(struct waiter
*w
)
136 struct task_struct
*task
;
142 wake_up_process(task
);
146 * We either wake a few readers or a single writer.
148 static void __wake_many(struct block_lock
*lock
)
150 struct waiter
*w
, *tmp
;
152 BUG_ON(lock
->count
< 0);
153 list_for_each_entry_safe(w
, tmp
, &lock
->waiters
, list
) {
154 if (lock
->count
>= MAX_HOLDERS
)
157 if (w
->wants_write
) {
159 return; /* still read locked */
162 __add_holder(lock
, w
->task
);
168 __add_holder(lock
, w
->task
);
173 static void bl_init(struct block_lock
*lock
)
177 spin_lock_init(&lock
->lock
);
179 INIT_LIST_HEAD(&lock
->waiters
);
180 for (i
= 0; i
< MAX_HOLDERS
; i
++)
181 lock
->holders
[i
] = NULL
;
184 static int __available_for_read(struct block_lock
*lock
)
186 return lock
->count
>= 0 &&
187 lock
->count
< MAX_HOLDERS
&&
188 list_empty(&lock
->waiters
);
191 static int bl_down_read(struct block_lock
*lock
)
196 spin_lock(&lock
->lock
);
197 r
= __check_holder(lock
);
199 spin_unlock(&lock
->lock
);
203 if (__available_for_read(lock
)) {
205 __add_holder(lock
, current
);
206 spin_unlock(&lock
->lock
);
210 get_task_struct(current
);
214 list_add_tail(&w
.list
, &lock
->waiters
);
215 spin_unlock(&lock
->lock
);
218 put_task_struct(current
);
222 static int bl_down_read_nonblock(struct block_lock
*lock
)
226 spin_lock(&lock
->lock
);
227 r
= __check_holder(lock
);
231 if (__available_for_read(lock
)) {
233 __add_holder(lock
, current
);
239 spin_unlock(&lock
->lock
);
243 static void bl_up_read(struct block_lock
*lock
)
245 spin_lock(&lock
->lock
);
246 BUG_ON(lock
->count
<= 0);
247 __del_holder(lock
, current
);
249 if (!list_empty(&lock
->waiters
))
251 spin_unlock(&lock
->lock
);
254 static int bl_down_write(struct block_lock
*lock
)
259 spin_lock(&lock
->lock
);
260 r
= __check_holder(lock
);
262 spin_unlock(&lock
->lock
);
266 if (lock
->count
== 0 && list_empty(&lock
->waiters
)) {
268 __add_holder(lock
, current
);
269 spin_unlock(&lock
->lock
);
273 get_task_struct(current
);
278 * Writers given priority. We know there's only one mutator in the
279 * system, so ignoring the ordering reversal.
281 list_add(&w
.list
, &lock
->waiters
);
282 spin_unlock(&lock
->lock
);
285 put_task_struct(current
);
290 static void bl_up_write(struct block_lock
*lock
)
292 spin_lock(&lock
->lock
);
293 __del_holder(lock
, current
);
295 if (!list_empty(&lock
->waiters
))
297 spin_unlock(&lock
->lock
);
300 static void report_recursive_bug(dm_block_t b
, int r
)
303 DMERR("recursive acquisition of block %llu requested.",
304 (unsigned long long) b
);
307 #else /* !CONFIG_DM_DEBUG_BLOCK_MANAGER_LOCKING */
309 #define bl_init(x) do { } while (0)
310 #define bl_down_read(x) 0
311 #define bl_down_read_nonblock(x) 0
312 #define bl_up_read(x) do { } while (0)
313 #define bl_down_write(x) 0
314 #define bl_up_write(x) do { } while (0)
315 #define report_recursive_bug(x, y) do { } while (0)
317 #endif /* CONFIG_DM_DEBUG_BLOCK_MANAGER_LOCKING */
319 /*----------------------------------------------------------------*/
322 * Block manager is currently implemented using dm-bufio. struct
323 * dm_block_manager and struct dm_block map directly onto a couple of
324 * structs in the bufio interface. I want to retain the freedom to move
325 * away from bufio in the future. So these structs are just cast within
326 * this .c file, rather than making it through to the public interface.
328 static struct dm_buffer
*to_buffer(struct dm_block
*b
)
330 return (struct dm_buffer
*) b
;
333 dm_block_t
dm_block_location(struct dm_block
*b
)
335 return dm_bufio_get_block_number(to_buffer(b
));
337 EXPORT_SYMBOL_GPL(dm_block_location
);
339 void *dm_block_data(struct dm_block
*b
)
341 return dm_bufio_get_block_data(to_buffer(b
));
343 EXPORT_SYMBOL_GPL(dm_block_data
);
346 struct dm_block_validator
*validator
;
349 #ifdef CONFIG_DM_DEBUG_BLOCK_MANAGER_LOCKING
350 struct block_lock lock
;
354 static void dm_block_manager_alloc_callback(struct dm_buffer
*buf
)
356 struct buffer_aux
*aux
= dm_bufio_get_aux_data(buf
);
357 aux
->validator
= NULL
;
361 static void dm_block_manager_write_callback(struct dm_buffer
*buf
)
363 struct buffer_aux
*aux
= dm_bufio_get_aux_data(buf
);
364 if (aux
->validator
) {
365 aux
->validator
->prepare_for_write(aux
->validator
, (struct dm_block
*) buf
,
366 dm_bufio_get_block_size(dm_bufio_get_client(buf
)));
370 /*----------------------------------------------------------------
372 *--------------------------------------------------------------*/
373 struct dm_block_manager
{
374 struct dm_bufio_client
*bufio
;
378 struct dm_block_manager
*dm_block_manager_create(struct block_device
*bdev
,
380 unsigned max_held_per_thread
)
383 struct dm_block_manager
*bm
;
385 bm
= kmalloc(sizeof(*bm
), GFP_KERNEL
);
391 bm
->bufio
= dm_bufio_client_create(bdev
, block_size
, max_held_per_thread
,
392 sizeof(struct buffer_aux
),
393 dm_block_manager_alloc_callback
,
394 dm_block_manager_write_callback
);
395 if (IS_ERR(bm
->bufio
)) {
396 r
= PTR_ERR(bm
->bufio
);
401 bm
->read_only
= false;
408 EXPORT_SYMBOL_GPL(dm_block_manager_create
);
410 void dm_block_manager_destroy(struct dm_block_manager
*bm
)
412 dm_bufio_client_destroy(bm
->bufio
);
415 EXPORT_SYMBOL_GPL(dm_block_manager_destroy
);
417 unsigned dm_bm_block_size(struct dm_block_manager
*bm
)
419 return dm_bufio_get_block_size(bm
->bufio
);
421 EXPORT_SYMBOL_GPL(dm_bm_block_size
);
423 dm_block_t
dm_bm_nr_blocks(struct dm_block_manager
*bm
)
425 return dm_bufio_get_device_size(bm
->bufio
);
428 static int dm_bm_validate_buffer(struct dm_block_manager
*bm
,
429 struct dm_buffer
*buf
,
430 struct buffer_aux
*aux
,
431 struct dm_block_validator
*v
)
433 if (unlikely(!aux
->validator
)) {
437 r
= v
->check(v
, (struct dm_block
*) buf
, dm_bufio_get_block_size(bm
->bufio
));
439 DMERR_LIMIT("%s validator check failed for block %llu", v
->name
,
440 (unsigned long long) dm_bufio_get_block_number(buf
));
445 if (unlikely(aux
->validator
!= v
)) {
446 DMERR_LIMIT("validator mismatch (old=%s vs new=%s) for block %llu",
447 aux
->validator
->name
, v
? v
->name
: "NULL",
448 (unsigned long long) dm_bufio_get_block_number(buf
));
455 int dm_bm_read_lock(struct dm_block_manager
*bm
, dm_block_t b
,
456 struct dm_block_validator
*v
,
457 struct dm_block
**result
)
459 struct buffer_aux
*aux
;
463 p
= dm_bufio_read(bm
->bufio
, b
, (struct dm_buffer
**) result
);
467 aux
= dm_bufio_get_aux_data(to_buffer(*result
));
468 r
= bl_down_read(&aux
->lock
);
470 dm_bufio_release(to_buffer(*result
));
471 report_recursive_bug(b
, r
);
475 aux
->write_locked
= 0;
477 r
= dm_bm_validate_buffer(bm
, to_buffer(*result
), aux
, v
);
479 bl_up_read(&aux
->lock
);
480 dm_bufio_release(to_buffer(*result
));
486 EXPORT_SYMBOL_GPL(dm_bm_read_lock
);
488 int dm_bm_write_lock(struct dm_block_manager
*bm
,
489 dm_block_t b
, struct dm_block_validator
*v
,
490 struct dm_block
**result
)
492 struct buffer_aux
*aux
;
496 if (dm_bm_is_read_only(bm
))
499 p
= dm_bufio_read(bm
->bufio
, b
, (struct dm_buffer
**) result
);
503 aux
= dm_bufio_get_aux_data(to_buffer(*result
));
504 r
= bl_down_write(&aux
->lock
);
506 dm_bufio_release(to_buffer(*result
));
507 report_recursive_bug(b
, r
);
511 aux
->write_locked
= 1;
513 r
= dm_bm_validate_buffer(bm
, to_buffer(*result
), aux
, v
);
515 bl_up_write(&aux
->lock
);
516 dm_bufio_release(to_buffer(*result
));
522 EXPORT_SYMBOL_GPL(dm_bm_write_lock
);
524 int dm_bm_read_try_lock(struct dm_block_manager
*bm
,
525 dm_block_t b
, struct dm_block_validator
*v
,
526 struct dm_block
**result
)
528 struct buffer_aux
*aux
;
532 p
= dm_bufio_get(bm
->bufio
, b
, (struct dm_buffer
**) result
);
538 aux
= dm_bufio_get_aux_data(to_buffer(*result
));
539 r
= bl_down_read_nonblock(&aux
->lock
);
541 dm_bufio_release(to_buffer(*result
));
542 report_recursive_bug(b
, r
);
545 aux
->write_locked
= 0;
547 r
= dm_bm_validate_buffer(bm
, to_buffer(*result
), aux
, v
);
549 bl_up_read(&aux
->lock
);
550 dm_bufio_release(to_buffer(*result
));
557 int dm_bm_write_lock_zero(struct dm_block_manager
*bm
,
558 dm_block_t b
, struct dm_block_validator
*v
,
559 struct dm_block
**result
)
562 struct buffer_aux
*aux
;
565 if (dm_bm_is_read_only(bm
))
568 p
= dm_bufio_new(bm
->bufio
, b
, (struct dm_buffer
**) result
);
572 memset(p
, 0, dm_bm_block_size(bm
));
574 aux
= dm_bufio_get_aux_data(to_buffer(*result
));
575 r
= bl_down_write(&aux
->lock
);
577 dm_bufio_release(to_buffer(*result
));
581 aux
->write_locked
= 1;
586 EXPORT_SYMBOL_GPL(dm_bm_write_lock_zero
);
588 void dm_bm_unlock(struct dm_block
*b
)
590 struct buffer_aux
*aux
;
591 aux
= dm_bufio_get_aux_data(to_buffer(b
));
593 if (aux
->write_locked
) {
594 dm_bufio_mark_buffer_dirty(to_buffer(b
));
595 bl_up_write(&aux
->lock
);
597 bl_up_read(&aux
->lock
);
599 dm_bufio_release(to_buffer(b
));
601 EXPORT_SYMBOL_GPL(dm_bm_unlock
);
603 int dm_bm_flush(struct dm_block_manager
*bm
)
605 if (dm_bm_is_read_only(bm
))
608 return dm_bufio_write_dirty_buffers(bm
->bufio
);
610 EXPORT_SYMBOL_GPL(dm_bm_flush
);
612 void dm_bm_prefetch(struct dm_block_manager
*bm
, dm_block_t b
)
614 dm_bufio_prefetch(bm
->bufio
, b
, 1);
617 bool dm_bm_is_read_only(struct dm_block_manager
*bm
)
619 return (bm
? bm
->read_only
: true);
621 EXPORT_SYMBOL_GPL(dm_bm_is_read_only
);
623 void dm_bm_set_read_only(struct dm_block_manager
*bm
)
626 bm
->read_only
= true;
628 EXPORT_SYMBOL_GPL(dm_bm_set_read_only
);
630 void dm_bm_set_read_write(struct dm_block_manager
*bm
)
633 bm
->read_only
= false;
635 EXPORT_SYMBOL_GPL(dm_bm_set_read_write
);
637 u32
dm_bm_checksum(const void *data
, size_t len
, u32 init_xor
)
639 return crc32c(~(u32
) 0, data
, len
) ^ init_xor
;
641 EXPORT_SYMBOL_GPL(dm_bm_checksum
);
643 /*----------------------------------------------------------------*/
645 MODULE_LICENSE("GPL");
646 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
647 MODULE_DESCRIPTION("Immutable metadata library for dm");
649 /*----------------------------------------------------------------*/