1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/slab.h>
10#include <net/sock.h>
11#include <linux/workqueue.h>
12#include <linux/connector.h>
13#include <linux/device-mapper.h>
14#include <linux/dm-log-userspace.h>
15
16#include "dm-log-userspace-transfer.h"
17
18static uint32_t dm_ulog_seq;
19
20
21
22
23
24
25
26#define DM_ULOG_RETRY_TIMEOUT (15 * HZ)
27
28
29
30
31#define DM_ULOG_PREALLOCED_SIZE 512
32static struct cn_msg *prealloced_cn_msg;
33static struct dm_ulog_request *prealloced_ulog_tfr;
34
35static struct cb_id ulog_cn_id = {
36 .idx = CN_IDX_DM,
37 .val = CN_VAL_DM_USERSPACE_LOG
38};
39
40static DEFINE_MUTEX(dm_ulog_lock);
41
42struct receiving_pkg {
43 struct list_head list;
44 struct completion complete;
45
46 uint32_t seq;
47
48 int error;
49 size_t *data_size;
50 char *data;
51};
52
53static DEFINE_SPINLOCK(receiving_list_lock);
54static struct list_head receiving_list;
55
56static int dm_ulog_sendto_server(struct dm_ulog_request *tfr)
57{
58 int r;
59 struct cn_msg *msg = prealloced_cn_msg;
60
61 memset(msg, 0, sizeof(struct cn_msg));
62
63 msg->id.idx = ulog_cn_id.idx;
64 msg->id.val = ulog_cn_id.val;
65 msg->ack = 0;
66 msg->seq = tfr->seq;
67 msg->len = sizeof(struct dm_ulog_request) + tfr->data_size;
68
69 r = cn_netlink_send(msg, 0, gfp_any());
70
71 return r;
72}
73
74
75
76
77
78
79
80
81
82static int fill_pkg(struct cn_msg *msg, struct dm_ulog_request *tfr)
83{
84 uint32_t rtn_seq = (msg) ? msg->seq : (tfr) ? tfr->seq : 0;
85 struct receiving_pkg *pkg;
86
87
88
89
90
91
92
93
94
95
96
97 list_for_each_entry(pkg, &receiving_list, list) {
98 if (rtn_seq != pkg->seq)
99 continue;
100
101 if (msg) {
102 pkg->error = -msg->ack;
103
104
105
106
107
108 if (pkg->error != -EAGAIN)
109 *(pkg->data_size) = 0;
110 } else if (tfr->data_size > *(pkg->data_size)) {
111 DMERR("Insufficient space to receive package [%u] "
112 "(%u vs %zu)", tfr->request_type,
113 tfr->data_size, *(pkg->data_size));
114
115 *(pkg->data_size) = 0;
116 pkg->error = -ENOSPC;
117 } else {
118 pkg->error = tfr->error;
119 memcpy(pkg->data, tfr->data, tfr->data_size);
120 *(pkg->data_size) = tfr->data_size;
121 }
122 complete(&pkg->complete);
123 return 0;
124 }
125
126 return -ENOENT;
127}
128
129
130
131
132
133static void cn_ulog_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
134{
135 struct dm_ulog_request *tfr = (struct dm_ulog_request *)(msg + 1);
136
137 if (!cap_raised(current_cap(), CAP_SYS_ADMIN))
138 return;
139
140 spin_lock(&receiving_list_lock);
141 if (msg->len == 0)
142 fill_pkg(msg, NULL);
143 else if (msg->len < sizeof(*tfr))
144 DMERR("Incomplete message received (expected %u, got %u): [%u]",
145 (unsigned)sizeof(*tfr), msg->len, msg->seq);
146 else
147 fill_pkg(NULL, tfr);
148 spin_unlock(&receiving_list_lock);
149}
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170int dm_consult_userspace(const char *uuid, uint64_t luid, int request_type,
171 char *data, size_t data_size,
172 char *rdata, size_t *rdata_size)
173{
174 int r = 0;
175 size_t dummy = 0;
176 int overhead_size = sizeof(struct dm_ulog_request) + sizeof(struct cn_msg);
177 struct dm_ulog_request *tfr = prealloced_ulog_tfr;
178 struct receiving_pkg pkg;
179
180
181
182
183
184
185 if (data_size > (DM_ULOG_PREALLOCED_SIZE - overhead_size)) {
186 DMINFO("Size of tfr exceeds preallocated size");
187 return -EINVAL;
188 }
189
190 if (!rdata_size)
191 rdata_size = &dummy;
192resend:
193
194
195
196
197 mutex_lock(&dm_ulog_lock);
198
199 memset(tfr, 0, DM_ULOG_PREALLOCED_SIZE - sizeof(struct cn_msg));
200 memcpy(tfr->uuid, uuid, DM_UUID_LEN);
201 tfr->version = DM_ULOG_REQUEST_VERSION;
202 tfr->luid = luid;
203 tfr->seq = dm_ulog_seq++;
204
205
206
207
208
209
210 tfr->request_type = request_type & DM_ULOG_REQUEST_MASK;
211
212 tfr->data_size = data_size;
213 if (data && data_size)
214 memcpy(tfr->data, data, data_size);
215
216 memset(&pkg, 0, sizeof(pkg));
217 init_completion(&pkg.complete);
218 pkg.seq = tfr->seq;
219 pkg.data_size = rdata_size;
220 pkg.data = rdata;
221 spin_lock(&receiving_list_lock);
222 list_add(&(pkg.list), &receiving_list);
223 spin_unlock(&receiving_list_lock);
224
225 r = dm_ulog_sendto_server(tfr);
226
227 mutex_unlock(&dm_ulog_lock);
228
229 if (r) {
230 DMERR("Unable to send log request [%u] to userspace: %d",
231 request_type, r);
232 spin_lock(&receiving_list_lock);
233 list_del_init(&(pkg.list));
234 spin_unlock(&receiving_list_lock);
235
236 goto out;
237 }
238
239 r = wait_for_completion_timeout(&(pkg.complete), DM_ULOG_RETRY_TIMEOUT);
240 spin_lock(&receiving_list_lock);
241 list_del_init(&(pkg.list));
242 spin_unlock(&receiving_list_lock);
243 if (!r) {
244 DMWARN("[%s] Request timed out: [%u/%u] - retrying",
245 (strlen(uuid) > 8) ?
246 (uuid + (strlen(uuid) - 8)) : (uuid),
247 request_type, pkg.seq);
248 goto resend;
249 }
250
251 r = pkg.error;
252 if (r == -EAGAIN)
253 goto resend;
254
255out:
256 return r;
257}
258
259int dm_ulog_tfr_init(void)
260{
261 int r;
262 void *prealloced;
263
264 INIT_LIST_HEAD(&receiving_list);
265
266 prealloced = kmalloc(DM_ULOG_PREALLOCED_SIZE, GFP_KERNEL);
267 if (!prealloced)
268 return -ENOMEM;
269
270 prealloced_cn_msg = prealloced;
271 prealloced_ulog_tfr = prealloced + sizeof(struct cn_msg);
272
273 r = cn_add_callback(&ulog_cn_id, "dmlogusr", cn_ulog_callback);
274 if (r) {
275 cn_del_callback(&ulog_cn_id);
276 return r;
277 }
278
279 return 0;
280}
281
282void dm_ulog_tfr_exit(void)
283{
284 cn_del_callback(&ulog_cn_id);
285 kfree(prealloced_cn_msg);
286}
287