// SPDX-License-Identifier: GPL-2.0-only /* * The Virtio 9p transport driver * * This is a block based transport driver based on the lguest block driver * code. * * Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation * * Based on virtio console driver * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
*/
/* a single mutex to manage channel initialization and attachment */ static DEFINE_MUTEX(virtio_9p_lock); static DECLARE_WAIT_QUEUE_HEAD(vp_wq); static atomic_t vp_pinned = ATOMIC_INIT(0);
/** * struct virtio_chan - per-instance transport information * @inuse: whether the channel is in use * @lock: protects multiple elements within this structure * @client: client instance * @vdev: virtio dev associated with this channel * @vq: virtio queue associated with this channel * @ring_bufs_avail: flag to indicate there is some available in the ring buf * @vc_wq: wait queue for waiting for thing to be added to ring buf * @p9_max_pages: maximum number of pinned pages * @sg: scatter gather list which is used to pack a request (protected?) * @chan_list: linked list of channels * * We keep all per-channel information in a structure. * This structure is allocated within the devices dev->mem space. * A pointer to the structure will get put in the transport private. *
*/
struct virtio_chan { bool inuse;
spinlock_t lock;
struct p9_client *client; struct virtio_device *vdev; struct virtqueue *vq; int ring_bufs_avail;
wait_queue_head_t *vc_wq; /* This is global limit. Since we don't have a global structure, * will be placing it in each channel.
*/ unsignedlong p9_max_pages; /* Scatterlist: can be too big for stack. */ struct scatterlist sg[VIRTQUEUE_NUM]; /** * @tag: name to identify a mount null terminated
*/ char *tag;
struct list_head chan_list;
};
staticstruct list_head virtio_chan_list;
/* How many bytes left in this page. */ staticunsignedint rest_of_page(void *data)
{ return PAGE_SIZE - offset_in_page(data);
}
/** * p9_virtio_close - reclaim resources of a channel * @client: client instance * * This reclaims a channel by freeing its resources and * resetting its inuse flag. *
*/
mutex_lock(&virtio_9p_lock); if (chan)
chan->inuse = false;
mutex_unlock(&virtio_9p_lock);
}
/** * req_done - callback which signals activity from the server * @vq: virtio queue activity was received on * * This notifies us that the server has triggered some activity * on the virtio channel - most likely a response to request we * sent. Figure out which requests now have responses and wake up * those threads. * * Bugs: could do with some additional sanity checking, but appears to work. *
*/
spin_lock_irqsave(&chan->lock, flags); while ((req = virtqueue_get_buf(chan->vq, &len)) != NULL) { if (!chan->ring_bufs_avail) {
chan->ring_bufs_avail = 1;
need_wakeup = true;
}
if (len) {
req->rc.size = len;
p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
}
}
spin_unlock_irqrestore(&chan->lock, flags); /* Wakeup if anyone waiting for VirtIO ring space. */ if (need_wakeup)
wake_up(chan->vc_wq);
}
/** * pack_sg_list - pack a scatter gather list from a linear buffer * @sg: scatter/gather list to pack into * @start: which segment of the sg_list to start at * @limit: maximum segment to pack data to * @data: data to pack into scatter/gather list * @count: amount of data to pack into the scatter/gather list * * sg_lists have multiple segments of various sizes. This will pack * arbitrary data into an existing scatter gather list, segmenting the * data as necessary within constraints. *
*/
staticint pack_sg_list(struct scatterlist *sg, int start, int limit, char *data, int count)
{ int s; int index = start;
while (count) {
s = rest_of_page(data); if (s > count)
s = count;
BUG_ON(index >= limit); /* Make sure we don't terminate early. */
sg_unmark_end(&sg[index]);
sg_set_buf(&sg[index++], data, s);
count -= s;
data += s;
} if (index-start)
sg_mark_end(&sg[index - 1]); return index-start;
}
/* We don't currently allow canceling of virtio requests */ staticint p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
{ return 1;
}
/** * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer, * this takes a list of pages. * @sg: scatter/gather list to pack into * @start: which segment of the sg_list to start at * @limit: maximum number of pages in sg list. * @pdata: a list of pages to add into sg. * @nr_pages: number of pages to pack into the scatter/gather list * @offs: amount of data in the beginning of first page _not_ to pack * @count: amount of data to pack into the scatter/gather list
*/ staticint
pack_sg_list_p(struct scatterlist *sg, int start, int limit, struct page **pdata, int nr_pages, size_t offs, int count)
{ int i = 0, s; int data_off = offs; int index = start;
BUG_ON(nr_pages > (limit - start)); /* * if the first page doesn't start at * page boundary find the offset
*/ while (nr_pages) {
s = PAGE_SIZE - data_off; if (s > count)
s = count;
BUG_ON(index >= limit); /* Make sure we don't terminate early. */
sg_unmark_end(&sg[index]);
sg_set_page(&sg[index++], pdata[i++], s, data_off);
data_off = 0;
count -= s;
nr_pages--;
}
if (index-start)
sg_mark_end(&sg[index - 1]); return index - start;
}
/** * p9_virtio_request - issue a request * @client: client instance issuing the request * @req: request to be issued *
*/
staticint
p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
{ int err; int in, out, out_sgs, in_sgs; unsignedlong flags; struct virtio_chan *chan = client->trans; struct scatterlist *sgs[2];
staticint p9_get_mapped_pages(struct virtio_chan *chan, struct page ***pages, struct iov_iter *data, int count,
size_t *offs, int *need_drop)
{ int nr_pages; int err;
if (!iov_iter_count(data)) return 0;
if (!iov_iter_is_kvec(data)) { int n; /* * We allow only p9_max_pages pinned. We wait for the * Other zc request to finish here
*/ if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
err = wait_event_killable(vp_wq,
(atomic_read(&vp_pinned) < chan->p9_max_pages)); if (err == -ERESTARTSYS) return err;
}
n = iov_iter_get_pages_alloc2(data, pages, count, offs); if (n < 0) return n;
*need_drop = 1;
nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
atomic_add(nr_pages, &vp_pinned); return n;
} else { /* kernel buffer, no need to pin pages */ int index;
size_t len; void *p;
/* we'd already checked that it's non-empty */ while (1) {
len = iov_iter_single_seg_count(data); if (likely(len)) {
p = data->kvec->iov_base + data->iov_offset; break;
}
iov_iter_advance(data, 0);
} if (len > count)
len = count;
// Fits entirely into the static data? Nothing to do. if (req->rc.size < in_hdr_len || !pages) return;
// Really long error message? Tough, truncate the reply. Might get // rejected (we can't be arsed to adjust the size encoded in header, // or string size for that matter), but it wouldn't be anything valid // anyway. if (unlikely(req->rc.size > P9_ZC_HDR_SZ))
req->rc.size = P9_ZC_HDR_SZ;
// data won't span more than two pages
size = req->rc.size - in_hdr_len;
n = PAGE_SIZE - offs; if (size > n) {
memcpy_from_page(to, *pages++, offs, n);
offs = 0;
to += n;
size -= n;
}
memcpy_from_page(to, *pages, offs, size);
}
/** * p9_virtio_zc_request - issue a zero copy request * @client: client instance issuing the request * @req: request to be issued * @uidata: user buffer that should be used for zero copy read * @uodata: user buffer that should be used for zero copy write * @inlen: read buffer size * @outlen: write buffer size * @in_hdr_len: reader header size, This is the size of response protocol data *
*/ staticint
p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req, struct iov_iter *uidata, struct iov_iter *uodata, int inlen, int outlen, int in_hdr_len)
{ int in, out, err, out_sgs, in_sgs; unsignedlong flags; int in_nr_pages = 0, out_nr_pages = 0; struct page **in_pages = NULL, **out_pages = NULL; struct virtio_chan *chan = client->trans; struct scatterlist *sgs[4];
size_t offs = 0; int need_drop = 0; int kicked = 0;
p9_debug(P9_DEBUG_TRANS, "virtio request\n");
if (uodata) {
__le32 sz; int n = p9_get_mapped_pages(chan, &out_pages, uodata,
outlen, &offs, &need_drop); if (n < 0) {
err = n; goto err_out;
}
out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE); if (n != outlen) {
__le32 v = cpu_to_le32(n);
memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
outlen = n;
} /* The size field of the message must include the length of the * header and the length of the data. We didn't actually know * the length of the data until this point so add it in now.
*/
sz = cpu_to_le32(req->tc.size + outlen);
memcpy(&req->tc.sdata[0], &sz, sizeof(sz));
} elseif (uidata) { int n = p9_get_mapped_pages(chan, &in_pages, uidata,
inlen, &offs, &need_drop); if (n < 0) {
err = n; goto err_out;
}
in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE); if (n != inlen) {
__le32 v = cpu_to_le32(n);
memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
inlen = n;
}
}
WRITE_ONCE(req->status, REQ_STATUS_SENT);
req_retry_pinned:
spin_lock_irqsave(&chan->lock, flags);
out_sgs = in_sgs = 0;
/* out data */
out = pack_sg_list(chan->sg, 0,
VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
if (out)
sgs[out_sgs++] = chan->sg;
if (out_pages) {
sgs[out_sgs++] = chan->sg + out;
out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
out_pages, out_nr_pages, offs, outlen);
}
/* * Take care of in data * For example TREAD have 11. * 11 is the read/write header = PDU Header(7) + IO Size (4). * Arrange in such a way that server places header in the * allocated memory and payload onto the user buffer.
*/
in = pack_sg_list(chan->sg, out,
VIRTQUEUE_NUM, req->rc.sdata, in_hdr_len); if (in)
sgs[out_sgs + in_sgs++] = chan->sg + out;
if (in_pages) {
sgs[out_sgs + in_sgs++] = chan->sg + out + in;
pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
in_pages, in_nr_pages, offs, inlen);
}
/** * p9_virtio_create - allocate a new virtio channel * @client: client instance invoking this transport * @devname: string identifying the channel to connect to (unused) * @args: args passed from sys_mount() for per-transport options (unused) * * This sets up a transport channel for 9p communication. Right now * we only match the first available channel, but eventually we could look up * alternate channels by matching devname versus a virtio_config entry. * We use a simple reference count mechanism to ensure that only a single * mount has a channel open at a time. *
*/
staticint
p9_virtio_create(struct p9_client *client, constchar *devname, char *args)
{ struct virtio_chan *chan; int ret = -ENOENT; int found = 0;
if (devname == NULL) return -EINVAL;
mutex_lock(&virtio_9p_lock);
list_for_each_entry(chan, &virtio_chan_list, chan_list) { if (!strcmp(devname, chan->tag)) { if (!chan->inuse) {
chan->inuse = true;
found = 1; break;
}
ret = -EBUSY;
}
}
mutex_unlock(&virtio_9p_lock);
if (!found) {
pr_err("no channels available for device %s\n", devname); return ret;
}
staticstruct p9_trans_module p9_virtio_trans = {
.name = "virtio",
.create = p9_virtio_create,
.close = p9_virtio_close,
.request = p9_virtio_request,
.zc_request = p9_virtio_zc_request,
.cancel = p9_virtio_cancel,
.cancelled = p9_virtio_cancelled, /* * We leave one entry for input and one entry for response * headers. We also skip one more entry to accommodate, address * that are not at page boundary, that can result in an extra * page in zero copy.
*/
.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
.pooled_rbuffers = false,
.def = 1,
.owner = THIS_MODULE,
};
/* The standard init function */ staticint __init p9_virtio_init(void)
{ int rc;
INIT_LIST_HEAD(&virtio_chan_list);
v9fs_register_trans(&p9_virtio_trans);
rc = register_virtio_driver(&p9_virtio_drv); if (rc)
v9fs_unregister_trans(&p9_virtio_trans);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.