]>
git.proxmox.com Git - mirror_qemu.git/blob - block/write-threshold.c
2 * QEMU System Emulator block write threshold notification
4 * Copyright Red Hat, Inc. 2014
7 * Francesco Romani <fromani@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "block/block_int.h"
15 #include "qemu/coroutine.h"
16 #include "block/write-threshold.h"
17 #include "qemu/notify.h"
18 #include "qapi/error.h"
19 #include "qapi/qapi-commands-block-core.h"
20 #include "qapi/qapi-events-block-core.h"
22 uint64_t bdrv_write_threshold_get(const BlockDriverState
*bs
)
24 return bs
->write_threshold_offset
;
27 bool bdrv_write_threshold_is_set(const BlockDriverState
*bs
)
29 return bs
->write_threshold_offset
> 0;
32 static void write_threshold_disable(BlockDriverState
*bs
)
34 if (bdrv_write_threshold_is_set(bs
)) {
35 notifier_with_return_remove(&bs
->write_threshold_notifier
);
36 bs
->write_threshold_offset
= 0;
40 uint64_t bdrv_write_threshold_exceeded(const BlockDriverState
*bs
,
41 const BdrvTrackedRequest
*req
)
43 if (bdrv_write_threshold_is_set(bs
)) {
44 if (req
->offset
> bs
->write_threshold_offset
) {
45 return (req
->offset
- bs
->write_threshold_offset
) + req
->bytes
;
47 if ((req
->offset
+ req
->bytes
) > bs
->write_threshold_offset
) {
48 return (req
->offset
+ req
->bytes
) - bs
->write_threshold_offset
;
54 static int coroutine_fn
before_write_notify(NotifierWithReturn
*notifier
,
57 BdrvTrackedRequest
*req
= opaque
;
58 BlockDriverState
*bs
= req
->bs
;
61 amount
= bdrv_write_threshold_exceeded(bs
, req
);
63 qapi_event_send_block_write_threshold(
66 bs
->write_threshold_offset
);
68 /* autodisable to avoid flooding the monitor */
69 write_threshold_disable(bs
);
72 return 0; /* should always let other notifiers run */
75 static void write_threshold_register_notifier(BlockDriverState
*bs
)
77 bs
->write_threshold_notifier
.notify
= before_write_notify
;
78 bdrv_add_before_write_notifier(bs
, &bs
->write_threshold_notifier
);
81 static void write_threshold_update(BlockDriverState
*bs
,
82 int64_t threshold_bytes
)
84 bs
->write_threshold_offset
= threshold_bytes
;
87 void bdrv_write_threshold_set(BlockDriverState
*bs
, uint64_t threshold_bytes
)
89 if (bdrv_write_threshold_is_set(bs
)) {
90 if (threshold_bytes
> 0) {
91 write_threshold_update(bs
, threshold_bytes
);
93 write_threshold_disable(bs
);
96 if (threshold_bytes
> 0) {
97 /* avoid multiple registration */
98 write_threshold_register_notifier(bs
);
99 write_threshold_update(bs
, threshold_bytes
);
101 /* discard bogus disable request */
105 void qmp_block_set_write_threshold(const char *node_name
,
106 uint64_t threshold_bytes
,
109 BlockDriverState
*bs
;
110 AioContext
*aio_context
;
112 bs
= bdrv_find_node(node_name
);
114 error_setg(errp
, "Device '%s' not found", node_name
);
118 aio_context
= bdrv_get_aio_context(bs
);
119 aio_context_acquire(aio_context
);
121 bdrv_write_threshold_set(bs
, threshold_bytes
);
123 aio_context_release(aio_context
);