xref: /qemu/block/write-threshold.c (revision 7a4e543d)
1 /*
2  * QEMU System Emulator block write threshold notification
3  *
4  * Copyright Red Hat, Inc. 2014
5  *
6  * Authors:
7  *  Francesco Romani <fromani@redhat.com>
8  *
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.
11  */
12 
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-event.h"
19 #include "qmp-commands.h"
20 
21 
22 uint64_t bdrv_write_threshold_get(const BlockDriverState *bs)
23 {
24     return bs->write_threshold_offset;
25 }
26 
27 bool bdrv_write_threshold_is_set(const BlockDriverState *bs)
28 {
29     return bs->write_threshold_offset > 0;
30 }
31 
32 static void write_threshold_disable(BlockDriverState *bs)
33 {
34     if (bdrv_write_threshold_is_set(bs)) {
35         notifier_with_return_remove(&bs->write_threshold_notifier);
36         bs->write_threshold_offset = 0;
37     }
38 }
39 
40 uint64_t bdrv_write_threshold_exceeded(const BlockDriverState *bs,
41                                        const BdrvTrackedRequest *req)
42 {
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;
46         }
47         if ((req->offset + req->bytes) > bs->write_threshold_offset) {
48             return (req->offset + req->bytes) - bs->write_threshold_offset;
49         }
50     }
51     return 0;
52 }
53 
54 static int coroutine_fn before_write_notify(NotifierWithReturn *notifier,
55                                             void *opaque)
56 {
57     BdrvTrackedRequest *req = opaque;
58     BlockDriverState *bs = req->bs;
59     uint64_t amount = 0;
60 
61     amount = bdrv_write_threshold_exceeded(bs, req);
62     if (amount > 0) {
63         qapi_event_send_block_write_threshold(
64             bs->node_name,
65             amount,
66             bs->write_threshold_offset,
67             &error_abort);
68 
69         /* autodisable to avoid flooding the monitor */
70         write_threshold_disable(bs);
71     }
72 
73     return 0; /* should always let other notifiers run */
74 }
75 
76 static void write_threshold_register_notifier(BlockDriverState *bs)
77 {
78     bs->write_threshold_notifier.notify = before_write_notify;
79     notifier_with_return_list_add(&bs->before_write_notifiers,
80                                   &bs->write_threshold_notifier);
81 }
82 
83 static void write_threshold_update(BlockDriverState *bs,
84                                    int64_t threshold_bytes)
85 {
86     bs->write_threshold_offset = threshold_bytes;
87 }
88 
89 void bdrv_write_threshold_set(BlockDriverState *bs, uint64_t threshold_bytes)
90 {
91     if (bdrv_write_threshold_is_set(bs)) {
92         if (threshold_bytes > 0) {
93             write_threshold_update(bs, threshold_bytes);
94         } else {
95             write_threshold_disable(bs);
96         }
97     } else {
98         if (threshold_bytes > 0) {
99             /* avoid multiple registration */
100             write_threshold_register_notifier(bs);
101             write_threshold_update(bs, threshold_bytes);
102         }
103         /* discard bogus disable request */
104     }
105 }
106 
107 void qmp_block_set_write_threshold(const char *node_name,
108                                    uint64_t threshold_bytes,
109                                    Error **errp)
110 {
111     BlockDriverState *bs;
112     AioContext *aio_context;
113 
114     bs = bdrv_find_node(node_name);
115     if (!bs) {
116         error_setg(errp, "Device '%s' not found", node_name);
117         return;
118     }
119 
120     aio_context = bdrv_get_aio_context(bs);
121     aio_context_acquire(aio_context);
122 
123     bdrv_write_threshold_set(bs, threshold_bytes);
124 
125     aio_context_release(aio_context);
126 }
127