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