Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2006-2009 Red Hat, Inc.
0003  *
0004  * This file is released under the LGPL.
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/slab.h>
0010 #include <net/sock.h>
0011 #include <linux/workqueue.h>
0012 #include <linux/connector.h>
0013 #include <linux/device-mapper.h>
0014 #include <linux/dm-log-userspace.h>
0015 
0016 #include "dm-log-userspace-transfer.h"
0017 
0018 static uint32_t dm_ulog_seq;
0019 
0020 /*
0021  * Netlink/Connector is an unreliable protocol.  How long should
0022  * we wait for a response before assuming it was lost and retrying?
0023  * (If we do receive a response after this time, it will be discarded
0024  * and the response to the resent request will be waited for.
0025  */
0026 #define DM_ULOG_RETRY_TIMEOUT (15 * HZ)
0027 
0028 /*
0029  * Pre-allocated space for speed
0030  */
0031 #define DM_ULOG_PREALLOCED_SIZE 512
0032 static struct cn_msg *prealloced_cn_msg;
0033 static struct dm_ulog_request *prealloced_ulog_tfr;
0034 
0035 static struct cb_id ulog_cn_id = {
0036     .idx = CN_IDX_DM,
0037     .val = CN_VAL_DM_USERSPACE_LOG
0038 };
0039 
0040 static DEFINE_MUTEX(dm_ulog_lock);
0041 
0042 struct receiving_pkg {
0043     struct list_head list;
0044     struct completion complete;
0045 
0046     uint32_t seq;
0047 
0048     int error;
0049     size_t *data_size;
0050     char *data;
0051 };
0052 
0053 static DEFINE_SPINLOCK(receiving_list_lock);
0054 static struct list_head receiving_list;
0055 
0056 static int dm_ulog_sendto_server(struct dm_ulog_request *tfr)
0057 {
0058     int r;
0059     struct cn_msg *msg = prealloced_cn_msg;
0060 
0061     memset(msg, 0, sizeof(struct cn_msg));
0062 
0063     msg->id.idx = ulog_cn_id.idx;
0064     msg->id.val = ulog_cn_id.val;
0065     msg->ack = 0;
0066     msg->seq = tfr->seq;
0067     msg->len = sizeof(struct dm_ulog_request) + tfr->data_size;
0068 
0069     r = cn_netlink_send(msg, 0, 0, gfp_any());
0070 
0071     return r;
0072 }
0073 
0074 /*
0075  * Parameters for this function can be either msg or tfr, but not
0076  * both.  This function fills in the reply for a waiting request.
0077  * If just msg is given, then the reply is simply an ACK from userspace
0078  * that the request was received.
0079  *
0080  * Returns: 0 on success, -ENOENT on failure
0081  */
0082 static int fill_pkg(struct cn_msg *msg, struct dm_ulog_request *tfr)
0083 {
0084     uint32_t rtn_seq = (msg) ? msg->seq : (tfr) ? tfr->seq : 0;
0085     struct receiving_pkg *pkg;
0086 
0087     /*
0088      * The 'receiving_pkg' entries in this list are statically
0089      * allocated on the stack in 'dm_consult_userspace'.
0090      * Each process that is waiting for a reply from the user
0091      * space server will have an entry in this list.
0092      *
0093      * We are safe to do it this way because the stack space
0094      * is unique to each process, but still addressable by
0095      * other processes.
0096      */
0097     list_for_each_entry(pkg, &receiving_list, list) {
0098         if (rtn_seq != pkg->seq)
0099             continue;
0100 
0101         if (msg) {
0102             pkg->error = -msg->ack;
0103             /*
0104              * If we are trying again, we will need to know our
0105              * storage capacity.  Otherwise, along with the
0106              * error code, we make explicit that we have no data.
0107              */
0108             if (pkg->error != -EAGAIN)
0109                 *(pkg->data_size) = 0;
0110         } else if (tfr->data_size > *(pkg->data_size)) {
0111             DMERR("Insufficient space to receive package [%u] "
0112                   "(%u vs %zu)", tfr->request_type,
0113                   tfr->data_size, *(pkg->data_size));
0114 
0115             *(pkg->data_size) = 0;
0116             pkg->error = -ENOSPC;
0117         } else {
0118             pkg->error = tfr->error;
0119             memcpy(pkg->data, tfr->data, tfr->data_size);
0120             *(pkg->data_size) = tfr->data_size;
0121         }
0122         complete(&pkg->complete);
0123         return 0;
0124     }
0125 
0126     return -ENOENT;
0127 }
0128 
0129 /*
0130  * This is the connector callback that delivers data
0131  * that was sent from userspace.
0132  */
0133 static void cn_ulog_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
0134 {
0135     struct dm_ulog_request *tfr = (struct dm_ulog_request *)(msg + 1);
0136 
0137     if (!capable(CAP_SYS_ADMIN))
0138         return;
0139 
0140     spin_lock(&receiving_list_lock);
0141     if (msg->len == 0)
0142         fill_pkg(msg, NULL);
0143     else if (msg->len < sizeof(*tfr))
0144         DMERR("Incomplete message received (expected %u, got %u): [%u]",
0145               (unsigned)sizeof(*tfr), msg->len, msg->seq);
0146     else
0147         fill_pkg(NULL, tfr);
0148     spin_unlock(&receiving_list_lock);
0149 }
0150 
0151 /**
0152  * dm_consult_userspace
0153  * @uuid: log's universal unique identifier (must be DM_UUID_LEN in size)
0154  * @luid: log's local unique identifier
0155  * @request_type:  found in include/linux/dm-log-userspace.h
0156  * @data: data to tx to the server
0157  * @data_size: size of data in bytes
0158  * @rdata: place to put return data from server
0159  * @rdata_size: value-result (amount of space given/amount of space used)
0160  *
0161  * rdata_size is undefined on failure.
0162  *
0163  * Memory used to communicate with userspace is zero'ed
0164  * before populating to ensure that no unwanted bits leak
0165  * from kernel space to user-space.  All userspace log communications
0166  * between kernel and user space go through this function.
0167  *
0168  * Returns: 0 on success, -EXXX on failure
0169  **/
0170 int dm_consult_userspace(const char *uuid, uint64_t luid, int request_type,
0171              char *data, size_t data_size,
0172              char *rdata, size_t *rdata_size)
0173 {
0174     int r = 0;
0175     unsigned long tmo;
0176     size_t dummy = 0;
0177     int overhead_size = sizeof(struct dm_ulog_request) + sizeof(struct cn_msg);
0178     struct dm_ulog_request *tfr = prealloced_ulog_tfr;
0179     struct receiving_pkg pkg;
0180 
0181     /*
0182      * Given the space needed to hold the 'struct cn_msg' and
0183      * 'struct dm_ulog_request' - do we have enough payload
0184      * space remaining?
0185      */
0186     if (data_size > (DM_ULOG_PREALLOCED_SIZE - overhead_size)) {
0187         DMINFO("Size of tfr exceeds preallocated size");
0188         return -EINVAL;
0189     }
0190 
0191     if (!rdata_size)
0192         rdata_size = &dummy;
0193 resend:
0194     /*
0195      * We serialize the sending of requests so we can
0196      * use the preallocated space.
0197      */
0198     mutex_lock(&dm_ulog_lock);
0199 
0200     memset(tfr, 0, DM_ULOG_PREALLOCED_SIZE - sizeof(struct cn_msg));
0201     memcpy(tfr->uuid, uuid, DM_UUID_LEN);
0202     tfr->version = DM_ULOG_REQUEST_VERSION;
0203     tfr->luid = luid;
0204     tfr->seq = dm_ulog_seq++;
0205 
0206     /*
0207      * Must be valid request type (all other bits set to
0208      * zero).  This reserves other bits for possible future
0209      * use.
0210      */
0211     tfr->request_type = request_type & DM_ULOG_REQUEST_MASK;
0212 
0213     tfr->data_size = data_size;
0214     if (data && data_size)
0215         memcpy(tfr->data, data, data_size);
0216 
0217     memset(&pkg, 0, sizeof(pkg));
0218     init_completion(&pkg.complete);
0219     pkg.seq = tfr->seq;
0220     pkg.data_size = rdata_size;
0221     pkg.data = rdata;
0222     spin_lock(&receiving_list_lock);
0223     list_add(&(pkg.list), &receiving_list);
0224     spin_unlock(&receiving_list_lock);
0225 
0226     r = dm_ulog_sendto_server(tfr);
0227 
0228     mutex_unlock(&dm_ulog_lock);
0229 
0230     if (r) {
0231         DMERR("Unable to send log request [%u] to userspace: %d",
0232               request_type, r);
0233         spin_lock(&receiving_list_lock);
0234         list_del_init(&(pkg.list));
0235         spin_unlock(&receiving_list_lock);
0236 
0237         goto out;
0238     }
0239 
0240     tmo = wait_for_completion_timeout(&(pkg.complete), DM_ULOG_RETRY_TIMEOUT);
0241     spin_lock(&receiving_list_lock);
0242     list_del_init(&(pkg.list));
0243     spin_unlock(&receiving_list_lock);
0244     if (!tmo) {
0245         DMWARN("[%s] Request timed out: [%u/%u] - retrying",
0246                (strlen(uuid) > 8) ?
0247                (uuid + (strlen(uuid) - 8)) : (uuid),
0248                request_type, pkg.seq);
0249         goto resend;
0250     }
0251 
0252     r = pkg.error;
0253     if (r == -EAGAIN)
0254         goto resend;
0255 
0256 out:
0257     return r;
0258 }
0259 
0260 int dm_ulog_tfr_init(void)
0261 {
0262     int r;
0263     void *prealloced;
0264 
0265     INIT_LIST_HEAD(&receiving_list);
0266 
0267     prealloced = kmalloc(DM_ULOG_PREALLOCED_SIZE, GFP_KERNEL);
0268     if (!prealloced)
0269         return -ENOMEM;
0270 
0271     prealloced_cn_msg = prealloced;
0272     prealloced_ulog_tfr = prealloced + sizeof(struct cn_msg);
0273 
0274     r = cn_add_callback(&ulog_cn_id, "dmlogusr", cn_ulog_callback);
0275     if (r) {
0276         kfree(prealloced_cn_msg);
0277         return r;
0278     }
0279 
0280     return 0;
0281 }
0282 
0283 void dm_ulog_tfr_exit(void)
0284 {
0285     cn_del_callback(&ulog_cn_id);
0286     kfree(prealloced_cn_msg);
0287 }