Path: blob/master/drivers/md/dm-log-userspace-transfer.c
15109 views
/*1* Copyright (C) 2006-2009 Red Hat, Inc.2*3* This file is released under the LGPL.4*/56#include <linux/kernel.h>7#include <linux/module.h>8#include <linux/slab.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>1415#include "dm-log-userspace-transfer.h"1617static uint32_t dm_ulog_seq;1819/*20* Netlink/Connector is an unreliable protocol. How long should21* 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 discarded23* and the response to the resent request will be waited for.24*/25#define DM_ULOG_RETRY_TIMEOUT (15 * HZ)2627/*28* Pre-allocated space for speed29*/30#define DM_ULOG_PREALLOCED_SIZE 51231static struct cn_msg *prealloced_cn_msg;32static struct dm_ulog_request *prealloced_ulog_tfr;3334static struct cb_id ulog_cn_id = {35.idx = CN_IDX_DM,36.val = CN_VAL_DM_USERSPACE_LOG37};3839static DEFINE_MUTEX(dm_ulog_lock);4041struct receiving_pkg {42struct list_head list;43struct completion complete;4445uint32_t seq;4647int error;48size_t *data_size;49char *data;50};5152static DEFINE_SPINLOCK(receiving_list_lock);53static struct list_head receiving_list;5455static int dm_ulog_sendto_server(struct dm_ulog_request *tfr)56{57int r;58struct cn_msg *msg = prealloced_cn_msg;5960memset(msg, 0, sizeof(struct cn_msg));6162msg->id.idx = ulog_cn_id.idx;63msg->id.val = ulog_cn_id.val;64msg->ack = 0;65msg->seq = tfr->seq;66msg->len = sizeof(struct dm_ulog_request) + tfr->data_size;6768r = cn_netlink_send(msg, 0, gfp_any());6970return r;71}7273/*74* Parameters for this function can be either msg or tfr, but not75* 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 userspace77* that the request was received.78*79* Returns: 0 on success, -ENOENT on failure80*/81static int fill_pkg(struct cn_msg *msg, struct dm_ulog_request *tfr)82{83uint32_t rtn_seq = (msg) ? msg->seq : (tfr) ? tfr->seq : 0;84struct receiving_pkg *pkg;8586/*87* The 'receiving_pkg' entries in this list are statically88* allocated on the stack in 'dm_consult_userspace'.89* Each process that is waiting for a reply from the user90* space server will have an entry in this list.91*92* We are safe to do it this way because the stack space93* is unique to each process, but still addressable by94* other processes.95*/96list_for_each_entry(pkg, &receiving_list, list) {97if (rtn_seq != pkg->seq)98continue;99100if (msg) {101pkg->error = -msg->ack;102/*103* If we are trying again, we will need to know our104* storage capacity. Otherwise, along with the105* error code, we make explicit that we have no data.106*/107if (pkg->error != -EAGAIN)108*(pkg->data_size) = 0;109} else if (tfr->data_size > *(pkg->data_size)) {110DMERR("Insufficient space to receive package [%u] "111"(%u vs %zu)", tfr->request_type,112tfr->data_size, *(pkg->data_size));113114*(pkg->data_size) = 0;115pkg->error = -ENOSPC;116} else {117pkg->error = tfr->error;118memcpy(pkg->data, tfr->data, tfr->data_size);119*(pkg->data_size) = tfr->data_size;120}121complete(&pkg->complete);122return 0;123}124125return -ENOENT;126}127128/*129* This is the connector callback that delivers data130* that was sent from userspace.131*/132static void cn_ulog_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)133{134struct dm_ulog_request *tfr = (struct dm_ulog_request *)(msg + 1);135136if (!cap_raised(current_cap(), CAP_SYS_ADMIN))137return;138139spin_lock(&receiving_list_lock);140if (msg->len == 0)141fill_pkg(msg, NULL);142else if (msg->len < sizeof(*tfr))143DMERR("Incomplete message received (expected %u, got %u): [%u]",144(unsigned)sizeof(*tfr), msg->len, msg->seq);145else146fill_pkg(NULL, tfr);147spin_unlock(&receiving_list_lock);148}149150/**151* dm_consult_userspace152* @uuid: log's universal unique identifier (must be DM_UUID_LEN in size)153* @luid: log's local unique identifier154* @request_type: found in include/linux/dm-log-userspace.h155* @data: data to tx to the server156* @data_size: size of data in bytes157* @rdata: place to put return data from server158* @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'ed163* before populating to ensure that no unwanted bits leak164* from kernel space to user-space. All userspace log communications165* between kernel and user space go through this function.166*167* Returns: 0 on success, -EXXX on failure168**/169int dm_consult_userspace(const char *uuid, uint64_t luid, int request_type,170char *data, size_t data_size,171char *rdata, size_t *rdata_size)172{173int r = 0;174size_t dummy = 0;175int overhead_size = sizeof(struct dm_ulog_request) + sizeof(struct cn_msg);176struct dm_ulog_request *tfr = prealloced_ulog_tfr;177struct receiving_pkg pkg;178179/*180* Given the space needed to hold the 'struct cn_msg' and181* 'struct dm_ulog_request' - do we have enough payload182* space remaining?183*/184if (data_size > (DM_ULOG_PREALLOCED_SIZE - overhead_size)) {185DMINFO("Size of tfr exceeds preallocated size");186return -EINVAL;187}188189if (!rdata_size)190rdata_size = &dummy;191resend:192/*193* We serialize the sending of requests so we can194* use the preallocated space.195*/196mutex_lock(&dm_ulog_lock);197198memset(tfr, 0, DM_ULOG_PREALLOCED_SIZE - sizeof(struct cn_msg));199memcpy(tfr->uuid, uuid, DM_UUID_LEN);200tfr->version = DM_ULOG_REQUEST_VERSION;201tfr->luid = luid;202tfr->seq = dm_ulog_seq++;203204/*205* Must be valid request type (all other bits set to206* zero). This reserves other bits for possible future207* use.208*/209tfr->request_type = request_type & DM_ULOG_REQUEST_MASK;210211tfr->data_size = data_size;212if (data && data_size)213memcpy(tfr->data, data, data_size);214215memset(&pkg, 0, sizeof(pkg));216init_completion(&pkg.complete);217pkg.seq = tfr->seq;218pkg.data_size = rdata_size;219pkg.data = rdata;220spin_lock(&receiving_list_lock);221list_add(&(pkg.list), &receiving_list);222spin_unlock(&receiving_list_lock);223224r = dm_ulog_sendto_server(tfr);225226mutex_unlock(&dm_ulog_lock);227228if (r) {229DMERR("Unable to send log request [%u] to userspace: %d",230request_type, r);231spin_lock(&receiving_list_lock);232list_del_init(&(pkg.list));233spin_unlock(&receiving_list_lock);234235goto out;236}237238r = wait_for_completion_timeout(&(pkg.complete), DM_ULOG_RETRY_TIMEOUT);239spin_lock(&receiving_list_lock);240list_del_init(&(pkg.list));241spin_unlock(&receiving_list_lock);242if (!r) {243DMWARN("[%s] Request timed out: [%u/%u] - retrying",244(strlen(uuid) > 8) ?245(uuid + (strlen(uuid) - 8)) : (uuid),246request_type, pkg.seq);247goto resend;248}249250r = pkg.error;251if (r == -EAGAIN)252goto resend;253254out:255return r;256}257258int dm_ulog_tfr_init(void)259{260int r;261void *prealloced;262263INIT_LIST_HEAD(&receiving_list);264265prealloced = kmalloc(DM_ULOG_PREALLOCED_SIZE, GFP_KERNEL);266if (!prealloced)267return -ENOMEM;268269prealloced_cn_msg = prealloced;270prealloced_ulog_tfr = prealloced + sizeof(struct cn_msg);271272r = cn_add_callback(&ulog_cn_id, "dmlogusr", cn_ulog_callback);273if (r) {274cn_del_callback(&ulog_cn_id);275return r;276}277278return 0;279}280281void dm_ulog_tfr_exit(void)282{283cn_del_callback(&ulog_cn_id);284kfree(prealloced_cn_msg);285}286287288