| 1 | |
| 2 | |
| 3 | * Copyright (C) 2006-2009 Red Hat, Inc.
|
| 4 | *
|
| 5 | * This file is released under the LGPL.
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/kernel.h>
|
| 9 | #include <linux/module.h>
|
| 10 | #include <linux/slab.h>
|
| 11 | #include <net/sock.h>
|
| 12 | #include <linux/workqueue.h>
|
| 13 | #include <linux/connector.h>
|
| 14 | #include <linux/device-mapper.h>
|
| 15 | #include <linux/dm-log-userspace.h>
|
| 16 |
|
| 17 | #include "dm-log-userspace-transfer.h"
|
| 18 |
|
| 19 | static uint32_t dm_ulog_seq;
|
| 20 |
|
| 21 | |
| 22 | * Netlink/Connector is an unreliable protocol. How long should
|
| 23 | * we wait for a response before assuming it was lost and retrying?
|
| 24 | * (If we do receive a response after this time, it will be discarded
|
| 25 | * and the response to the resent request will be waited for.
|
| 26 |
|
| 27 | #define DM_ULOG_RETRY_TIMEOUT (15 * HZ)
|
| 28 |
|
| 29 | |
| 30 | * Pre-allocated space for speed
|
| 31 |
|
| 32 | #define DM_ULOG_PREALLOCED_SIZE 512
|
| 33 | static struct cn_msg *prealloced_cn_msg;
|
| 34 | static struct dm_ulog_request *prealloced_ulog_tfr;
|
| 35 |
|
| 36 | static struct cb_id ulog_cn_id = {
|
| 37 | .idx = CN_IDX_DM,
|
| 38 | .val = CN_VAL_DM_USERSPACE_LOG
|
| 39 | };
|
| 40 |
|
| 41 | static DEFINE_MUTEX(dm_ulog_lock);
|
| 42 |
|
| 43 | struct receiving_pkg {
|
| 44 | struct list_head list;
|
| 45 | struct completion complete;
|
| 46 |
|
| 47 | uint32_t seq;
|
| 48 |
|
| 49 | int error;
|
| 50 | size_t *data_size;
|
| 51 | char *data;
|
| 52 | };
|
| 53 |
|
| 54 | static DEFINE_SPINLOCK(receiving_list_lock);
|
| 55 | static struct list_head receiving_list;
|
| 56 |
|
| 57 | static int dm_ulog_sendto_server(struct dm_ulog_request *tfr)
|
| 58 | {
|
| 59 | int r;
|
| 60 | struct cn_msg *msg = prealloced_cn_msg;
|
| 61 |
|
| 62 | memset(msg, 0, sizeof(struct cn_msg));
|
| 63 |
|
| 64 | msg->id.idx = ulog_cn_id.idx;
|
| 65 | msg->id.val = ulog_cn_id.val;
|
| 66 | msg->ack = 0;
|
| 67 | msg->seq = tfr->seq;
|
| 68 | msg->len = sizeof(struct dm_ulog_request) + tfr->data_size;
|
| 69 |
|
| 70 | r = cn_netlink_send(msg, 0, 0, gfp_any());
|
| 71 |
|
| 72 | return r;
|
| 73 | }
|
| 74 |
|
| 75 | |
| 76 | * Parameters for this function can be either msg or tfr, but not
|
| 77 | * both. This function fills in the reply for a waiting request.
|
| 78 | * If just msg is given, then the reply is simply an ACK from userspace
|
| 79 | * that the request was received.
|
| 80 | *
|
| 81 | * Returns: 0 on success, -ENOENT on failure
|
| 82 |
|
| 83 | static int fill_pkg(struct cn_msg *msg, struct dm_ulog_request *tfr)
|
| 84 | {
|
| 85 | uint32_t rtn_seq = (msg) ? msg->seq : (tfr) ? tfr->seq : 0;
|
| 86 | struct receiving_pkg *pkg;
|
| 87 |
|
| 88 | |
| 89 | * The 'receiving_pkg' entries in this list are statically
|
| 90 | * allocated on the stack in 'dm_consult_userspace'.
|
| 91 | * Each process that is waiting for a reply from the user
|
| 92 | * space server will have an entry in this list.
|
| 93 | *
|
| 94 | * We are safe to do it this way because the stack space
|
| 95 | * is unique to each process, but still addressable by
|
| 96 | * other processes.
|
| 97 |
|
| 98 | list_for_each_entry(pkg, &receiving_list, list) {
|
| 99 | if (rtn_seq != pkg->seq)
|
| 100 | continue;
|
| 101 |
|
| 102 | if (msg) {
|
| 103 | pkg->error = -msg->ack;
|
| 104 | |
| 105 | * If we are trying again, we will need to know our
|
| 106 | * storage capacity. Otherwise, along with the
|
| 107 | * error code, we make explicit that we have no data.
|
| 108 |
|
| 109 | if (pkg->error != -EAGAIN)
|
| 110 | *(pkg->data_size) = 0;
|
| 111 | } else if (tfr->data_size > *(pkg->data_size)) {
|
| 112 | DMERR("Insufficient space to receive package [%u] (%u vs %zu)",
|
| 113 | tfr->request_type, 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 | * This is the connector callback that delivers data
|
| 131 | * that was sent from userspace.
|
| 132 |
|
| 133 | static 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 (!capable(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 int)sizeof(*tfr), msg->len, msg->seq);
|
| 146 | else
|
| 147 | fill_pkg(NULL, tfr);
|
| 148 | spin_unlock(&receiving_list_lock);
|
| 149 | }
|
| 150 |
|
| 151 | |
| 152 | * dm_consult_userspace
|
| 153 | * @uuid: log's universal unique identifier (must be DM_UUID_LEN in size)
|
| 154 | * @luid: log's local unique identifier
|
| 155 | * @request_type: found in include/linux/dm-log-userspace.h
|
| 156 | * @data: data to tx to the server
|
| 157 | * @data_size: size of data in bytes
|
| 158 | * @rdata: place to put return data from server
|
| 159 | * @rdata_size: value-result (amount of space given/amount of space used)
|
| 160 | *
|
| 161 | * rdata_size is undefined on failure.
|
| 162 | *
|
| 163 | * Memory used to communicate with userspace is zero'ed
|
| 164 | * before populating to ensure that no unwanted bits leak
|
| 165 | * from kernel space to user-space. All userspace log communications
|
| 166 | * between kernel and user space go through this function.
|
| 167 | *
|
| 168 | * Returns: 0 on success, -EXXX on failure
|
| 169 | *
|
| 170 | int 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 | unsigned long tmo;
|
| 176 | size_t dummy = 0;
|
| 177 | int overhead_size = sizeof(struct dm_ulog_request) + sizeof(struct cn_msg);
|
| 178 | struct dm_ulog_request *tfr = prealloced_ulog_tfr;
|
| 179 | struct receiving_pkg pkg;
|
| 180 |
|
| 181 | |
| 182 | * Given the space needed to hold the 'struct cn_msg' and
|
| 183 | * 'struct dm_ulog_request' - do we have enough payload
|
| 184 | * space remaining?
|
| 185 |
|
| 186 | if (data_size > (DM_ULOG_PREALLOCED_SIZE - overhead_size)) {
|
| 187 | DMINFO("Size of tfr exceeds preallocated size");
|
| 188 | return -EINVAL;
|
| 189 | }
|
| 190 |
|
| 191 | if (!rdata_size)
|
| 192 | rdata_size = &dummy;
|
| 193 | resend:
|
| 194 | |
| 195 | * We serialize the sending of requests so we can
|
| 196 | * use the preallocated space.
|
| 197 |
|
| 198 | mutex_lock(&dm_ulog_lock);
|
| 199 |
|
| 200 | memset(tfr, 0, DM_ULOG_PREALLOCED_SIZE - sizeof(struct cn_msg));
|
| 201 | memcpy(tfr->uuid, uuid, DM_UUID_LEN);
|
| 202 | tfr->version = DM_ULOG_REQUEST_VERSION;
|
| 203 | tfr->luid = luid;
|
| 204 | tfr->seq = dm_ulog_seq++;
|
| 205 |
|
| 206 | |
| 207 | * Must be valid request type (all other bits set to
|
| 208 | * zero). This reserves other bits for possible future
|
| 209 | * use.
|
| 210 |
|
| 211 | tfr->request_type = request_type & DM_ULOG_REQUEST_MASK;
|
| 212 |
|
| 213 | tfr->data_size = data_size;
|
| 214 | if (data && data_size)
|
| 215 | memcpy(tfr->data, data, data_size);
|
| 216 |
|
| 217 | memset(&pkg, 0, sizeof(pkg));
|
| 218 | init_completion(&pkg.complete);
|
| 219 | pkg.seq = tfr->seq;
|
| 220 | pkg.data_size = rdata_size;
|
| 221 | pkg.data = rdata;
|
| 222 | spin_lock(&receiving_list_lock);
|
| 223 | list_add(&(pkg.list), &receiving_list);
|
| 224 | spin_unlock(&receiving_list_lock);
|
| 225 |
|
| 226 | r = dm_ulog_sendto_server(tfr);
|
| 227 |
|
| 228 | mutex_unlock(&dm_ulog_lock);
|
| 229 |
|
| 230 | if (r) {
|
| 231 | DMERR("Unable to send log request [%u] to userspace: %d",
|
| 232 | request_type, r);
|
| 233 | spin_lock(&receiving_list_lock);
|
| 234 | list_del_init(&(pkg.list));
|
| 235 | spin_unlock(&receiving_list_lock);
|
| 236 |
|
| 237 | goto out;
|
| 238 | }
|
| 239 |
|
| 240 | tmo = wait_for_completion_timeout(&(pkg.complete), DM_ULOG_RETRY_TIMEOUT);
|
| 241 | spin_lock(&receiving_list_lock);
|
| 242 | list_del_init(&(pkg.list));
|
| 243 | spin_unlock(&receiving_list_lock);
|
| 244 | if (!tmo) {
|
| 245 | DMWARN("[%s] Request timed out: [%u/%u] - retrying",
|
| 246 | (strlen(uuid) > 8) ?
|
| 247 | (uuid + (strlen(uuid) - 8)) : (uuid),
|
| 248 | request_type, pkg.seq);
|
| 249 | goto resend;
|
| 250 | }
|
| 251 |
|
| 252 | r = pkg.error;
|
| 253 | if (r == -EAGAIN)
|
| 254 | goto resend;
|
| 255 |
|
| 256 | out:
|
| 257 | return r;
|
| 258 | }
|
| 259 |
|
| 260 | int dm_ulog_tfr_init(void)
|
| 261 | {
|
| 262 | int r;
|
| 263 | void *prealloced;
|
| 264 |
|
| 265 | INIT_LIST_HEAD(&receiving_list);
|
| 266 |
|
| 267 | prealloced = kmalloc(DM_ULOG_PREALLOCED_SIZE, GFP_KERNEL);
|
| 268 | if (!prealloced)
|
| 269 | return -ENOMEM;
|
| 270 |
|
| 271 | prealloced_cn_msg = prealloced;
|
| 272 | prealloced_ulog_tfr = prealloced + sizeof(struct cn_msg);
|
| 273 |
|
| 274 | r = cn_add_callback(&ulog_cn_id, "dmlogusr", cn_ulog_callback);
|
| 275 | if (r) {
|
| 276 | kfree(prealloced_cn_msg);
|
| 277 | return r;
|
| 278 | }
|
| 279 |
|
| 280 | return 0;
|
| 281 | }
|
| 282 |
|
| 283 | void dm_ulog_tfr_exit(void)
|
| 284 | {
|
| 285 | cn_del_callback(&ulog_cn_id);
|
| 286 | kfree(prealloced_cn_msg);
|
| 287 | }
|