dump: allow target to set the physical base
[qemu/ar7.git] / block / write-threshold.c
blob0fe38917c58e1793ea77acfaee44fb08edf35a5d
1 /*
2 * QEMU System Emulator block write threshold notification
4 * Copyright Red Hat, Inc. 2014
6 * Authors:
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 "block/block_int.h"
14 #include "qemu/coroutine.h"
15 #include "block/write-threshold.h"
16 #include "qemu/notify.h"
17 #include "qapi-event.h"
18 #include "qmp-commands.h"
21 uint64_t bdrv_write_threshold_get(const BlockDriverState *bs)
23 return bs->write_threshold_offset;
26 bool bdrv_write_threshold_is_set(const BlockDriverState *bs)
28 return bs->write_threshold_offset > 0;
31 static void write_threshold_disable(BlockDriverState *bs)
33 if (bdrv_write_threshold_is_set(bs)) {
34 notifier_with_return_remove(&bs->write_threshold_notifier);
35 bs->write_threshold_offset = 0;
39 uint64_t bdrv_write_threshold_exceeded(const BlockDriverState *bs,
40 const BdrvTrackedRequest *req)
42 if (bdrv_write_threshold_is_set(bs)) {
43 if (req->offset > bs->write_threshold_offset) {
44 return (req->offset - bs->write_threshold_offset) + req->bytes;
46 if ((req->offset + req->bytes) > bs->write_threshold_offset) {
47 return (req->offset + req->bytes) - bs->write_threshold_offset;
50 return 0;
53 static int coroutine_fn before_write_notify(NotifierWithReturn *notifier,
54 void *opaque)
56 BdrvTrackedRequest *req = opaque;
57 BlockDriverState *bs = req->bs;
58 uint64_t amount = 0;
60 amount = bdrv_write_threshold_exceeded(bs, req);
61 if (amount > 0) {
62 qapi_event_send_block_write_threshold(
63 bs->node_name,
64 amount,
65 bs->write_threshold_offset,
66 &error_abort);
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 notifier_with_return_list_add(&bs->before_write_notifiers,
79 &bs->write_threshold_notifier);
82 static void write_threshold_update(BlockDriverState *bs,
83 int64_t threshold_bytes)
85 bs->write_threshold_offset = threshold_bytes;
88 void bdrv_write_threshold_set(BlockDriverState *bs, uint64_t threshold_bytes)
90 if (bdrv_write_threshold_is_set(bs)) {
91 if (threshold_bytes > 0) {
92 write_threshold_update(bs, threshold_bytes);
93 } else {
94 write_threshold_disable(bs);
96 } else {
97 if (threshold_bytes > 0) {
98 /* avoid multiple registration */
99 write_threshold_register_notifier(bs);
100 write_threshold_update(bs, threshold_bytes);
102 /* discard bogus disable request */
106 void qmp_block_set_write_threshold(const char *node_name,
107 uint64_t threshold_bytes,
108 Error **errp)
110 BlockDriverState *bs;
111 AioContext *aio_context;
113 bs = bdrv_find_node(node_name);
114 if (!bs) {
115 error_setg(errp, "Device '%s' not found", node_name);
116 return;
119 aio_context = bdrv_get_aio_context(bs);
120 aio_context_acquire(aio_context);
122 bdrv_write_threshold_set(bs, threshold_bytes);
124 aio_context_release(aio_context);